【问题标题】:WPF User Control Item Control Binding IssueWPF 用户控件项控件绑定问题
【发布时间】:2011-08-25 12:29:22
【问题描述】:

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

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

【问题讨论】:

    标签: wpf user-controls


    【解决方案1】:

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

    class CheckModel
    {
       public bool Checked {get;set;}
       public string Description {get;set;}
    }
    

    如果您的数据动态更改并且您必须在 UI 中显示此更改,则让 CheckModel 实现 INotifyPropertyChanged。

    然后在主模型中创建一个ObservableCollection<CheckModel> 并将您的用户控件绑定到它。使用一些策略来映射CheckModel 实例中的底层真实数据。

    【讨论】:

      【解决方案2】:

      您必须在您的用户控件中创建 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>
      

      【讨论】:

        【解决方案3】:

        我的 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 是 Employee 类型,具有 EmployeeName、Age、Gender 属性

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

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

        【讨论】:

          猜你喜欢
          • 2010-12-09
          • 2011-04-07
          • 2010-12-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-06
          相关资源
          最近更新 更多