【发布时间】:2019-06-03 04:39:27
【问题描述】:
我需要在主窗口的ucComboBox 中填充ComboBox(通过在xaml 代码中使用ComboBoxItem),但我得到了这两个错误:
- 对象
ucComboBox已有子对象,无法添加ComboBoxItem。ucComboBox只能接受一个孩子。 -
Content属性只能设置一次。
这个问题没有完整的答案 "Adding ComboBoxItem to a combobox inside a user control (XAML/WPF)"
<Window x:Class="CustomComboBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CustomComboBox"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<ComboBox Grid.Row="0">
<ComboBoxItem>Male</ComboBoxItem>
<ComboBoxItem>Female</ComboBoxItem>
</ComboBox>
<local:ucComboBox Grid.Row="1">
<ComboBoxItem>Male</ComboBoxItem>
<ComboBoxItem>Female</ComboBoxItem>
</local:ucComboBox>
</Grid>
<UserControl x:Class="CustomComboBox.ucComboBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:CustomComboBox"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="100">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<ComboBox Grid.Row="0">
</ComboBox>
</Grid>
【问题讨论】:
-
拥有
ucComboBox有什么意义,而里面除了 ComboBox 什么都没有?为什么不创建一个派生的 ComboBox? -
它不仅仅是一个带有 ComboBox 的 UserControl。我试图尽可能简单地解释我的问题并删除所有不相关的东西。
标签: wpf data-binding wpf-controls