C# WPF Combobox DIsplayMemberPath 不显示
C# WPF Combobox DIsplayMemberPath not displaying
似乎遇到了一个问题,我试图设置一个对象的日期 属性 以在组合框选择中显示,代码运行时没有抱怨,并且在单步执行代码时似乎设置 属性 但是当构建应用程序时,选择选项显示空白行。
C#代码:
List<Item> items = new List<Item>();
Item item = new Item(itemDate ,incidentDate, incidentSeverity, incidentDescription);
items.Add(item);
foreach (Item x in items)
{
SelectItemComboBox.Items.Add(x);
SelectItemComboBox.DisplayMemberPath = x.ItemDate.ToShortDateString();
}
}
XAML:
<ComboBox Name="SelectItemComboBox" HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="150"/>
我想我可能需要从 XAML 代码进行某种数据绑定,但我不确定为什么我需要这样做,如果我删除了它会显示的 DisplayMemberPath 代码对象详细信息,而不是基础值,因此我不确定为什么它不再显示从调试角度来看似乎按预期执行的附加行的任何内容。
任何建议将不胜感激,感谢您的宝贵时间。
List<Item> items= new List<Item>();
Item item = new Item(itemDate,incidentDate,incidentSeverity, incidentDescription);
items.Add(item);
SelectItemComboBox.ItemsSource = items;
// You need to set the NAME of the property from Item class.
// For example, "ItemDate":
SelectItemComboBox.DisplayMemberPath = "itemDate";
似乎遇到了一个问题,我试图设置一个对象的日期 属性 以在组合框选择中显示,代码运行时没有抱怨,并且在单步执行代码时似乎设置 属性 但是当构建应用程序时,选择选项显示空白行。
C#代码:
List<Item> items = new List<Item>();
Item item = new Item(itemDate ,incidentDate, incidentSeverity, incidentDescription);
items.Add(item);
foreach (Item x in items)
{
SelectItemComboBox.Items.Add(x);
SelectItemComboBox.DisplayMemberPath = x.ItemDate.ToShortDateString();
}
}
XAML:
<ComboBox Name="SelectItemComboBox" HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="150"/>
我想我可能需要从 XAML 代码进行某种数据绑定,但我不确定为什么我需要这样做,如果我删除了它会显示的 DisplayMemberPath 代码对象详细信息,而不是基础值,因此我不确定为什么它不再显示从调试角度来看似乎按预期执行的附加行的任何内容。
任何建议将不胜感激,感谢您的宝贵时间。
List<Item> items= new List<Item>();
Item item = new Item(itemDate,incidentDate,incidentSeverity, incidentDescription);
items.Add(item);
SelectItemComboBox.ItemsSource = items;
// You need to set the NAME of the property from Item class.
// For example, "ItemDate":
SelectItemComboBox.DisplayMemberPath = "itemDate";