2012-04-18 45 views
2

我想了解如何使用数组的ItemSourceC#WPF列表框dispaly多个memberpath

到目前为止,我可以用一个价值管理在列表框中显示更多的价值,但无法弄清楚如何显示多个值:

代码:

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

toReturn.Groups是项目阵列;每个项目都有一个ID和一个名字。

我希望能够同时显示两者。你为什么不创建一个名为显示

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

然后在您的项目属性

+0

我可以看看代码的其余部分,我可以帮助你更好。我会举一个我最近使用过的例子。 – 2012-04-19 14:35:12

回答

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; 

Ofcourse我使用一个DataGrid,但我相信同样可以为列表框的工作?希望这可以帮助Davide。

2

只是做:

listBox1.DisplayMemberPath = "Display"; 
2

或者,你可以申请一个DataTemplate你的列表框的项目,以获得更多的控制权了解它们的显示方式,而不是将冗余属性添加到视图模型中。请在这里找到一个例子:http://www.wpftutorial.net/ListBoxDataTemplate.html