2015-08-14 179 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" />