我正在遵循Kelly Elias的优秀文章制作WPF checkListBox。
然而,在我的情况下,我必须使用反射在代码中创建对象。 ListBox项目源适当地填充,数据模板正确地为ListBox设置样式,导致复选框列表,但是CheckBox没有显示任何内容。 我在数据模板中绑定方面做错了什么?
为简洁起见,请参阅上面的链接以获取CheckedListItem类;我的未更改。
我们将键入CheckedListItem的Charge类:
然而,在我的情况下,我必须使用反射在代码中创建对象。 ListBox项目源适当地填充,数据模板正确地为ListBox设置样式,导致复选框列表,但是CheckBox没有显示任何内容。 我在数据模板中绑定方面做错了什么?
为简洁起见,请参阅上面的链接以获取CheckedListItem类;我的未更改。
我们将键入CheckedListItem的Charge类:
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 (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"。
感谢您的帮助!