【发布时间】:2015-05-05 16:29:13
【问题描述】:
我正在关注 Kelly Elias 在making a WPF checkListBox 上的精彩文章。
但是,就我而言,我必须使用反射在代码中创建对象。 ListBox 项目源正在正确填充,并且数据模板正确设置了 ListBox 的样式,从而生成了一个 CheckBox 列表,但没有为 CheckBox 显示任何内容。我的数据模板中的绑定有什么问题?
为简洁起见,请参阅上面的 CheckedListItem 类链接;我的没变。
Charge 类,我们将输入 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;
}
XAML 中的数据模板:
<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”。
感谢您的帮助!
【问题讨论】:
-
您现在可以发布图片了。 :)
-
谢谢OhBeWise,更新了!
标签: c# wpf xaml checkbox listbox