简体   繁体   中英

WPF ListBox with CheckBox data template - Binding Content property of Checkbox not working

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

4费用

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM