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

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

问题描述

我正在关注Kelly Elias关于制作的精彩文章一个WPF checkListBox



但是,在我的例子中,我必须使用反射创建我的代码。 ListBox项源代码正确填充,数据模板正确设置了ListBox的样式,产生了一个CheckBox列表,但没有为CheckBox显示任何内容。



为简洁起见,请参阅上面的CheckedListItem类的链接;

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

XAML中的DataTemplate

 < 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(participant.Charges中的收费)
{
Charges.Add(new CheckedListItem& ;(收费));
}

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

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

结果 b



底层Charges的ChargeSectionNumber属性的值为11418(b)(1),10,11和13。

在执行DataBinding时,您的类需要实现 INotifyPropertyChanged 使数据在UI中正确显示。例如:

  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));
}
}

公共事件PropertyChangedEventHandler PropertyChanged;
}

这显示了类,一个属性(ChargeSectionNumber)用于实现 INotifyPropertyChanged



在您的问题中引用的示例中,您可以看到,也实现 INotifyPropertyChanged


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

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?

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

The Charge class, of which we will type 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;
}

The DataTemplate in XAML:

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

The code behind:

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"];
}

The Result

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

Thank you for your assistance!

解决方案

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

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

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

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

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