将ComboBoxItem添加到UserControl内部的ComboBox(XAML / WPF)

时间:2019-06-03 04:39:27

标签: wpf data-binding wpf-controls

我需要在主窗口的ComboBox内填充ucComboBox(通过在xaml代码内使用ComboBoxItem),但是出现以下两个错误:

  1. 对象ucComboBox已经有一个孩子,不能添加ComboBoxItemucComboBox只能接受一个孩子。
  2. 属性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>

2 个答案:

答案 0 :(得分:0)

ucComboBox 不是一个ComboBox。它具有一个ComboBox。它是UserControl。这样就不能在其内容中添加任何ComboBoxItem

一个想法是从ucComboBox继承ComboBox。而且,不要使用Content中的ucComboBox,而要使用Items

PS :“ Adding ComboBoxItem to a combobox inside a user control (XAML/WPF)”不是您的答案。

最良好的祝愿。

答案 1 :(得分:0)

我找到了解决问题的方法。这是代码:

<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"
         Loaded="UserControl_Loaded"
         mc:Ignorable="d" 
         d:DesignHeight="50" d:DesignWidth="300">

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label  Grid.Row="0" Grid.Column="0">User Control</Label>
    <ComboBox Name="cmb" Grid.Row="0" Grid.Column="1" >
    </ComboBox>
</Grid>

    public partial class ucComboBox : UserControl
{
    public ucComboBox()
    {
        InitializeComponent();
    }

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        cmb.ItemsSource = ValueList;
    }

    public List<ComboBoxItem> ValueList
    {
        get { return (List<ComboBoxItem>)GetValue(ValueListProperty); }
        set { SetValue(ValueListProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ValueList.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ValueListProperty =
    DependencyProperty.Register("ValueList", typeof(List<ComboBoxItem>), typeof(ucComboBox), new FrameworkPropertyMetadata(new List<ComboBoxItem>(), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(Target)));

    private static void Target(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
    }

}


<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 />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="40" />
        <RowDefinition Height="40" />
    </Grid.RowDefinitions>
    <ComboBox Grid.Row="0" Grid.Column="1">
        <ComboBoxItem>Male</ComboBoxItem>
        <ComboBoxItem>Female</ComboBoxItem>
    </ComboBox>

    <local:ucComboBox x:Name="cmb" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"  >
        <local:ucComboBox.ValueList>
            <ComboBoxItem>All</ComboBoxItem>
            <ComboBoxItem>Male</ComboBoxItem>
            <ComboBoxItem>Female</ComboBoxItem>
        </local:ucComboBox.ValueList>
    </local:ucComboBox>
</Grid>

相关问题