C#wpf列表框显示多个成员路径

时间:2012-04-18 09:39:56

标签: c# .net wpf

我想了解如何使用数组作为itemsource

在列表框中显示更多值

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

代码:

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

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

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

3 个答案:

答案 0 :(得分:2)

为什么不在名为display

的项目上创建属性
public string Display {
    get {
        return String.Format("{0} - {1}", ID, Name);
    }
}

然后就这样做:

listBox1.DisplayMemberPath = "Display";

答案 1 :(得分:2)

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

答案 2 :(得分: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,但我确信同样适用于listbox?希望这能帮助Davide。

相关问题