3

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 Charges

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

Thank you for your assistance!

Bobby
  • 467
  • 4
  • 13

1 Answers1

2

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.

Stewbob
  • 16,759
  • 9
  • 63
  • 107
  • You are absolutely right! I should have seen that myself. Thank you for your timely and thorough answer. – Bobby May 05 '15 at 19:05
  • Just for clarity to anyone else looking at this, the Customer class identified in the referenced article above also did not implement INotifyPropertyChanged. This was a bug that the author intends to fix for future visitors. – Bobby May 07 '15 at 17:05