我在WPF中创建了一个用户控件,它是一种Item控件。此控件将有两列显示复选框,另一列将显示与该复选框相对应的描述。将此控件部署到WPF表单时,它将绑定到一个集合。根据收集项目,复选框的数量将显示在项目控件中,另一列项目控件将与收集的某些属性绑定。WPF用户控件项控件绑定问题
现在这里的问题是,我怎样才能在复选框描述列的用户控件上定义绑定?在用户控制上,我们不知道收集的属性,它可能是任何东西。
我在WPF中创建了一个用户控件,它是一种Item控件。此控件将有两列显示复选框,另一列将显示与该复选框相对应的描述。将此控件部署到WPF表单时,它将绑定到一个集合。根据收集项目,复选框的数量将显示在项目控件中,另一列项目控件将与收集的某些属性绑定。WPF用户控件项控件绑定问题
现在这里的问题是,我怎样才能在复选框描述列的用户控件上定义绑定?在用户控制上,我们不知道收集的属性,它可能是任何东西。
您应该创建一个模型类,一个辅助类来表示您的数据。有两个属性创建例如类:
class CheckModel
{
public bool Checked {get;set;}
public string Description {get;set;}
}
让CheckModel implemets INotifyPropertyChanged
如果你的数据是动态变化的,你必须表明这改变了用户界面。
然后在主模型中创建一个ObservableCollection<CheckModel>
并将您的用户控件绑定到它。使用一些策略在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();
}
}
而且你的用户控件的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>
如果您发现我已经绑定复选框,文本框我已经在用户控件创建的依赖属性。
当您使用的用户控件可以绑定的DependencyProperty
<Grid>
<local:UserControl1 IsChecked="{Binding yourBinding}" Text="{Binding yourBinding}"/>
</Grid>
我的CheckBoxList用户控件看起来像这样
<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>
在此用户控件的ItemsSource和CheckBoxDisplayColumn是DP的,这我想从我的客户端应用程序设置如下
<userControl35:CheckBoxListControl Grid.Row="6" Grid.Column="0"
ItemsSource="{Binding personObservableCollection}"
CheckBoxDisplayColumn="EmployeeName"></userControl35:CheckBoxListControl>
personObservableCollection是具有型员工属性EmployeeName,年龄,性别的
我想通过“EmployeeName”提供的DP CheckBoxDisplayColumn在客户端应用程序成为这我CheckBoxList控件用于显示复选框作为反对性别属性的方式或年龄
基本上,我想使控制通用,以便使用它的用户可以指定要绑定到集合的集合以及从集合绑定到的显示列