简体   繁体   English

WPF XAML绑定列表和组合框

[英]WPF XAML Binding List and Combobox

I am teaching myself how to bind classes to XAML objects. 我正在自学如何将类绑定到XAML对象。 I can't find anything on data within lists. 我在列表中的数据上找不到任何内容。 Either that or I don't know the terminology very well. 要么那个,要么我不太了解该术语。 I want to make a combobox that is tied to the list, displaying the name of each Item in the Items list. 我想制作一个与列表绑定的组合框,在“项目”列表中显示每个项目的名称。 How would I bind this to the combobox? 我如何将其绑定到组合框?

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

class Item
{
    private string _name;

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

Assuming Section is the current DataContext : 假设Section是当前的DataContext:

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

Try This, 尝试这个,

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

Make your Items collection as a property. 使您的Items集合成为属性。

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

Section Class should be public and make it as your DataContext Section类应该是public并使其成为您的DataContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM