【问题标题】:Adding ComboBoxItem to a ComboBox inside a UserControl (XAML/WPF)将 ComboBoxItem 添加到 UserControl (XAML/WPF) 内的 ComboBox
【发布时间】:2019-06-03 04:39:27
【问题描述】:

我需要在主窗口的ucComboBox 中填充ComboBox(通过在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>

【问题讨论】:

  • 拥有ucComboBox 有什么意义,而里面除了 ComboBox 什么都没有?为什么不创建一个派生的 ComboBox?
  • 它不仅仅是一个带有 ComboBox 的 UserControl。我试图尽可能简单地解释我的问题并删除所有不相关的东西。

标签: wpf data-binding wpf-controls


【解决方案1】:

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

一个想法是从ComboBox 继承ucComboBox。并且,不要使用ContentucComboBox,而是使用Items

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

最好的祝愿。

【讨论】:

    【解决方案2】:

    我找到了解决问题的方法。代码如下:

    <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>
    

    【讨论】:

    • 请注意,您的 ValueList 依赖属性的默认值存在问题。您将new List&lt;ComboBoxItem&gt;() 指定为默认值,但由于依赖属性注册是静态的,因此该值用于控件的所有 实例。除了 null 之外,您绝不能将任何其他内容指定为引用类型依赖属性的默认值。而是在控件构造函数中分配默认值。
    • 也没有必要使用List&lt;ComboBoxItem&gt; 作为属性类型。改用IEnumerable 会更灵活,因为这是“目标”ItemsSource 属性的类型。然后,您将能够分配任何类型的集合,例如字符串列表。
    猜你喜欢
    • 2011-02-16
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多