2015-08-14 178 views
1

我在教自己如何將類綁定到XAML對象。我無法找到列表中的任何數據。無論是我還是我都不熟悉這個術語。我想製作一個與列表綁定的組合框,在Items列表中顯示每個Item的名稱。我將如何將它綁定到組合框?WPF XAML綁定列表和組合框

class Section 
{ 
    List<Item> Items = new List<Item>(); 
} 

class Item 
{ 
    private string _name; 

    public string Name 
    { 
     get { return _name; } 
     set { _name = value; } 
    } 
} 
+0

Xiaoy是正確的,但如果你想要一個集合中的變化(添加和移除項目,你也應該改變你的列表和的ObservableCollection實例)複製到組合框中 –

+0

謝謝!我會看看如何使用這些! – GFocus

回答

2

試試這個,

<ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="Name" /> 

讓你收集的項目作爲屬性。

public List<Item> Items { get; set;} 

Section類應該是public,並使其爲你DataContext

+1

謝謝你的工作! – GFocus

+1

問題在於讓部分獲得;組;。將其重新轉換爲= new並且工作正常。 – GFocus

+0

正確的綁定僅適用於Properties屬性 –

2

假設Section是當前的DataContext:

<ComboBox ItemsSource="{Binding Items}" 
      DisplayMemberPath="Name" />