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!