WPF列表框使用复选框数据模板 - 复选框的工作不绑定Content属性 [英] WPF ListBox with CheckBox data template - Binding Content property of Checkbox not working

查看:179
本文介绍了WPF列表框使用复选框数据模板 - 复选框的工作不绑定Content属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面凯利Elias的上制作优秀的文章WPF checkListBox

I am following Kelly Elias' excellent article on making a WPF checkListBox.

不过,在我的情况,我有使用反射的代码创建我的对象。列表框项目源被适当地填充和所述数据模板正确样式列表框,导致复选框列表,但正在显示对CheckBox没有内容。什么是我错了,在我的数据模板的绑定做

However, in my case, I have to create my objects in code using reflection. The ListBox item source is populating appropriately and the data template is styling the ListBox correctly, resulting in a list of CheckBoxes, but no Content is being shown for the CheckBox. What am I doing wrong with the binding in my data template?

为了简洁这里,见上面的CheckedListItem类的链接?; 。我的是不变的。

For brevity here, see the link above for the CheckedListItem class; mine is unchanged.

的班主任,其中我们将键入CheckedListItem:

public class Charge
{
    public int ChargeId;
    public int ParticipantId;
    public int Count;
    public string ChargeSectionCode;
    public string ChargeSectionNumber;
    public string ChargeSectionDescription;
    public DateTime OffenseDate;
}



的DataTemplate中的XAML:

<UserControl.Resources>
    <DataTemplate x:Key="checkedListBoxTemplate">
        <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Path=Item.ChargeSectionNumber}" />
    </DataTemplate>
</UserControl.Resources>



背后的代码:

CaseParticipant participant = _caseParticipants.Where(q => q.ParticipantRole == content.SeedDataWherePath).FirstOrDefault();
ObservableCollection<CheckedListItem<Charge>> Charges = new ObservableCollection<CheckedListItem<Charge>>();

if (participant != null)
{
    foreach (Charge charge in participant.Charges)
    {
        Charges.Add(new CheckedListItem<Charge>(charge));
    }

    ((ListBox)control).DataContext = Charges;
    Binding b = new Binding() { Source = Charges };

    ((ListBox)control).SetBinding(ListBox.ItemsSourceProperty, b);
    ((ListBox)control).ItemTemplate = (DataTemplate)Resources["checkedListBoxTemplate"];
}



结果

标的收费的ChargeSectionNumber属性的值分别为11418(b)(1),10,11和13。

The ChargeSectionNumber property of the underlying Charges have the values "11418(b)(1)", "10", "11" and "13".

感谢您对于你的帮助!

推荐答案

在做数据绑定,你的类需要实现 INotifyPropertyChanged的的数据,在用户界面中正确显示。举个例子:

When doing DataBinding, your class needs to implement INotifyPropertyChanged for the data to properly display in the UI. An example:

public class Charge : INotifyPropertyChanged
{

  private string chargeSectionNumber;
  public string ChargeSectionNumber
  {
    get
    {
      return chargeSectionNumber;
    }
    set
    {
      if (value != chargeSectionNumber)
      {
        chargeSectionNumber = value;
        NotifyPropertyChanged("ChargeSectionNumber");
      }
    }
  }

  private void NotifyPropertyChanged(string info)
  {
    if (PropertyChanged != null)
    {
      PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;
}

这显示了类,一个属性(ChargeSectionNumber)和所需事件和方法实施 INotifyPropertyChanged的

This shows the class, one property (ChargeSectionNumber) and the needed event and method for implementing INotifyPropertyChanged.

在你在你的问题中引用的例子,你可以看到类绑定到还实现了 INotifyPropertyChanged的

In the example you referenced in your question, you can see that the class being bound to also implements INotifyPropertyChanged.

这篇关于WPF列表框使用复选框数据模板 - 复选框的工作不绑定Content属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆