C# wpf 列表框显示多个成员路径
本文关键字:成员 路径 显示 wpf 列表 | 更新日期: 2023-09-27 18:31:25
我想了解如何使用数组作为 itemsource 在列表框上显示更多值
到目前为止,我可以使用一个值进行管理,但无法弄清楚如何显示多个值:
法典:
listBox1.ItemsSource = toReturn.groups;
listBox1.DisplayMemberPath = "name";
toReturn.Groups
是项数组;每个项都有一个 ID 和一个名称。
我希望能够同时显示两者。
为什么不在项目上创建一个名为 display 的属性
public string Display {
get {
return String.Format("{0} - {1}", ID, Name);
}
}
然后只需做:
listBox1.DisplayMemberPath = "Display";
或者,可以将 DataTemplate 应用于列表框项,以便更好地控制它们的显示方式,而不是向视图模型添加冗余属性。请在此处找到示例:http://www.wpftutorial.net/ListBoxDataTemplate.html
string uriGroup = "http://localhost:8000/Service/Group"; //rest based http GET
private void button14_Click(object sender, EventArgs e)
{
XDocument xDoc = XDocument.Load(uriGroup); //Load the uri into the xdoc
var groups = xDoc.Descendants("Group") //descendants of xml query "Group"
.Select(n => new
{
GroupID = n.Element("GroudID").Value,
Group = n.Element("GroupName").Value, //first "Group" Sets the column title, second sets the Element to be listed in this case "GroupName" from my service.
})
.ToList();
dataGridView2.DataSource = groups;
当然,我使用数据网格,但我相信同样可以适用于列表框?希望这对大卫有所帮助。