簡體   English   中英

C#WPF列表框顯示多個成員路徑

[英]C# wpf listbox dispaly more than one memberpath

我想了解如何使用數組作為項源在列表框上顯示更多值

到目前為止,我可以使用一個值進行管理,但無法弄清楚如何顯示多個值:

碼:

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

toReturn.Groups是項目數組; 每個項目都有一個ID和一個Name。

我希望能夠同時顯示兩者。

為什么不在商品上創建名為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;

當然,我使用了dataGrid,但是我確定同樣適用於列表框嗎? 希望這對大衛有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM