【发布时间】:2017-03-01 18:41:08
【问题描述】:
我试图弄清楚如何绑定 ListBoxItems 列表并在我的控件上显示正确的 DisplayMemberPath。我需要使用 ListBoxItems 列表,因为我的控件正在访问诸如 ActualHeight/Width 之类的属性,如果我将 ItemsSource 绑定到状态列表,我会丢失这些属性
public class State
{
public string LongName { get; set; }
public string ShortName { get; set; }
}
我有一个 AllItems,一个状态列表。我正在填充我的 ListBox 的 ItemsSource,如下所示:
BoxItems = new ObservableCollection<MyListBoxItem>(AllItems.Select(i => new MyListBoxItem() { Content = i }).ToList());
我希望我的 DisplayMemberPath 为 LongName。我尝试了许多不同的方法来修改 .xaml 以正确显示 LongName(纽约、新泽西、佛罗里达等),但我的列表框中总是以 namespace.classname 结束。我尝试在 .xaml 和 .xaml.cs 中设置 ListBox 的 DisplayMemberPath 和 ItemsSource。
我尝试了许多不同的方法,例如设置内容控件和在 .xaml 中添加数据模板。
【问题讨论】:
-
'Content = i.LongName'
-
我正在尝试制作一个通用控件,它接受
List<object>,所以我不知道字段的名称。我公开了另一个属性,用于设置 DisplayMemberPath。
标签: c# wpf xaml listbox listboxitem