2

我想了解如何使用数组作为 itemsource 在列表框中显示更多值

到目前为止,我可以使用一个值进行管理,但无法弄清楚如何显示多个值:

代码:

        listBox1.ItemsSource = toReturn.groups;
        listBox1.DisplayMemberPath = "name";

toReturn.Groups是项目数组;每个项目都有一个 ID 和一个名称。

我希望能够同时显示两者。

4

3 回答 3

2

你为什么不在你的项目上创建一个名为 display 的属性

public string Display {
    get {
        return String.Format("{0} - {1}", ID, Name);
    }
}

然后做:

listBox1.DisplayMemberPath = "Display";
于 2012-04-18T09:43:33.473 回答
2

或者,您可以将 DataTemplate 应用于列表框项,以更好地控制它们的显示方式,而不是向视图模型添加冗余属性。请在此处查找示例:http: //www.wpftutorial.net/ListBoxDataTemplate.html

于 2012-04-18T09:47:26.200 回答
1
    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;

当然我使用dataGrid,但我确定列表框同样适用?希望这可以帮助大卫。

于 2012-04-19T14:41:15.933 回答