将集合绑定到组合框 [英] Bind collection to combobox

查看:38
本文介绍了将集合绑定到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个组合框

<ComboBox  Height="30" SelectedIndex="0" Margin="5 3 5 3" Width="170" ItemsSource="{Binding WonderList}" SelectedValuePath="selectedWonder">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <WrapPanel>
                <Image Source="{Binding Path}" Height="20"></Image>
                <Label Content="{Binding Name}" Style="{StaticResource LabelComboItem}"></Label>
            </WrapPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

我要在其中将项目显示为图像和文本.

where I want to show items as an image plus a text.

这是项目列表中对象的业务类

This is the business class for the objects in the item list

public class Wonder: INotifyPropertyChanged
{
    private string name;
    private string path;
    public event PropertyChangedEventHandler PropertyChanged;

    #region properties, getters and setters
    public String Name { get; set; }
    public String Path { get; set; }
    #endregion

    public Wonder(string name, string path)
    {
        this.name = name;
        this.path = path;
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

以及窗口后面的代码

public class Window1 {
    public List<Wonder> WonderList;

    public Window1()
    {
        InitializeComponent();
        WonderList = new List<Wonder>();
        WonderList.Add(new Wonder("Alexandria", "Resources/Images/Scans/Wonders/Alexandria.jpg"));
        WonderList.Add(new Wonder("Babylon", "Resources/Images/Scans/Wonders/Babylon.jpg"));
    }
}

我对这个xaml魔术"还很陌生,我想我不正确地理解数据绑定的工作原理,我认为使用 ItemsSource ="{Binding WonderList}" 时应该使用该集合该名称(来自后面的代码)并显示其名称和路径,但显示一个空列表.

I´m pretty new to this xaml "magic" and guess I dont understand correctly how the data binding works, I think that with ItemsSource="{Binding WonderList}" it should take the collection with that name (from the code behind) and show their Name and Path, but it shows an empty list.

如果我在后面的代码中执行了 Combo1.ItemsSource = WonderList; (我更喜欢使用xaml并避免使用后面的代码),它会显示两个空白位置,但仍然不知道如何显示项目.

If I do Combo1.ItemsSource = WonderList; in the code behind (I prefer to use the xaml and avoid the code behind), it shows two blank slots but still don´t know how to show the items.

您能指出我正确的方向吗?

Can you point me in the right direction?

谢谢

推荐答案

如果要这样绑定 ItemsSource ="{Binding WonderList}" ,则必须设置

If you want to bind like this ItemsSource="{Binding WonderList}" you have to set the DataContext first.

public Window1()
{
    ...
    this.DataContext = this;
}

然后,绑定将在Window1中找到WonderList,但前提是它也是一个属性.

Then Binding will find the WonderList in Window1 but only if it is a property too.

public List<Wonder> WonderList { get; private set; }

下一步:如果您将值分配给私有字段名称,则绑定到属性Name是没有用的.将您的构造函数替换为

Next: It is useless to bind to property Name if you assign your value to private field name. Replace your constructor with

public Wonder(string name, string path)
{
    this.Name = name;
    this.Path = path;
}

下一步:您的自动属性( {get; set;} )不会通知更改.为此,您必须在setter中调用 OnPropertyChanged .例如

Next: Your auto properties ({ get; set; }) will not notify for changes. For this you have to call OnPropertyChanged in setter. e.g.

public String Name
{
    get { return name; }
    set
    {
        if (name == value) return;
        name = value;
        OnPropertyChanged("Name");
    }
}

与WonderList相同.如果您在构造函数的后期创建列表,则可能是所有绑定都已解决,您什么也看不到.

Same thing for WonderList. If you create the List to late in constructor it could be all bindings are already resolved and you see nothing.

最后使用 ObservableCollection 您想通知的不是新列表,而是通知列表中新添加的项目.

And finally use ObservableCollection if you want to notify not for a new list but a new added item in your list.

这篇关于将集合绑定到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆