WPF用户控件项控件绑定问题

WPF用户控件项控件绑定问题,wpf,user-controls,Wpf,User Controls,我正在WPF中创建一个用户控件,它是项控件的一种类型。此控件将有两列,一列显示复选框,另一列显示与复选框对应的说明。将此控件部署到WPF表单时,它将与集合绑定。根据集合项,项控件中将显示“数目”复选框,项控件的另一列将与集合的某些属性绑定 现在这里的问题是,如何为复选框描述列定义用户控件上的绑定?在用户控件上,我们不知道集合的属性,它可以是任何东西。您应该创建一个模型类,一个助手类来表示您的数据。例如,创建一个具有两个属性的类: class CheckModel { public bool

我正在WPF中创建一个用户控件,它是项控件的一种类型。此控件将有两列,一列显示复选框,另一列显示与复选框对应的说明。将此控件部署到WPF表单时,它将与集合绑定。根据集合项,项控件中将显示“数目”复选框,项控件的另一列将与集合的某些属性绑定


现在这里的问题是,如何为复选框描述列定义用户控件上的绑定?在用户控件上,我们不知道集合的属性,它可以是任何东西。

您应该创建一个模型类,一个助手类来表示您的数据。例如,创建一个具有两个属性的类:

class CheckModel
{
   public bool Checked {get;set;}
   public string Description {get;set;}
}
如果您的数据动态更改,并且您必须在UI中显示此更改,则让CheckModel执行
INotifyPropertyChanged


然后在主模型中创建一个
可观测集合
,并将用户控件绑定到它。使用一些策略来映射
CheckModel
实例中的底层真实数据。

您必须在用户控件中创建DependencyProperty,该属性将用于绑定复选框和说明。像这样的东西

public partial class UserControl1 : UserControl
    {


        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool), typeof(UserControl1), new UIPropertyMetadata(false));



        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new UIPropertyMetadata(string.Empty));


        public UserControl1()
        {
            InitializeComponent();

        }     

    }
您的usercontrol xaml如下所示

<UserControl x:Class="WpfApplication1.UserControl1"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Name="Root">

    <StackPanel>
        <CheckBox IsChecked="{Binding IsChecked,ElementName=Root}"></CheckBox>
        <TextBlock Text="{Binding Text,ElementName=Root}"></TextBlock>
    </StackPanel>
</UserControl>

如果您注意到我已将复选框和文本框绑定到我在用户控件中创建的依赖项属性

使用usercontrol时,可以绑定DependencyProperty

<Grid>
                <local:UserControl1 IsChecked="{Binding yourBinding}" Text="{Binding yourBinding}"/>
            </Grid>

我的复选框列表用户控件如下所示

<UserControl x:Class="CheckBoxListControl.CheckBoxListControl"
         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" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<ScrollViewer  VerticalScrollBarVisibility="Auto">
    <StackPanel>
        <ListBox x:Name="CheckedListBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <CheckBox x:Name="templateCheckBox" Content="{Binding CheckBoxDisplayColumn}" VerticalAlignment="Center"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>
</ScrollViewer>

在这个用户控件项上,Source和CheckBoxDisplayColumn是DP,我想从我的客户端应用程序中设置它,如下所示

<userControl35:CheckBoxListControl Grid.Row="6" Grid.Column="0" 
                                       ItemsSource="{Binding personObservableCollection}"
                                       CheckBoxDisplayColumn="EmployeeName"></userControl35:CheckBoxListControl>

personObservableCollection属于Employee类型,具有属性EmployeeName、Age、Severy

我想要一种方式,使客户端应用程序上提供给DP CheckBoxDisplayColumn的“EmployeeName”成为我的CheckBoxList控件用于显示针对性别或年龄的复选框的属性

基本上,我希望使控件成为通用控件,以便使用它的用户可以指定要绑定到的集合以及要从集合绑定到的显示列