[英]C# wpf listbox dispaly more than one memberpath
I'd like to understand how to display more values on a listbox using an array as itemsource 我想了解如何使用数组作为项源在列表框上显示更多值
So far I can manage with one value, but cannot figure out how to display more than one value: 到目前为止,我可以使用一个值进行管理,但无法弄清楚如何显示多个值:
Code: 码:
listBox1.ItemsSource = toReturn.groups;
listBox1.DisplayMemberPath = "name";
toReturn.Groups
is the array of items; toReturn.Groups
是项目数组; every item has an ID and a Name. 每个项目都有一个ID和一个Name。
I would like to be able to display both. 我希望能够同时显示两者。
Why don't you create a property on your items called display 为什么不在商品上创建名为display的属性
public string Display {
get {
return String.Format("{0} - {1}", ID, Name);
}
}
Then just do: 然后做:
listBox1.DisplayMemberPath = "Display";
Alternatively, you can apply a DataTemplate to your listbox items to gain more control over how they are displayed and not to add redundant properties to your view model. 或者,您可以将DataTemplate应用于列表框项目,以更好地控制它们的显示方式,而不必向视图模型添加多余的属性。 Please find an example here: http://www.wpftutorial.net/ListBoxDataTemplate.html 请在此处找到示例: 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;
Ofcourse I use a dataGrid but I am sure the same can work for listbox? 当然,我使用了dataGrid,但是我确定同样适用于列表框吗? Hope this helps Davide. 希望这对大卫有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.