【问题标题】:Bind collection to combobox将集合绑定到组合框
【发布时间】:2013-06-23 21:18:08
【问题描述】:

我有这个组合框

<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>

我想将项目显示为图像和文本。

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

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}" 它应该采用具有该名称的集合(来自后面的代码)并显示他们的名称和路径,但它显示一个空列表。

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

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

谢谢

【问题讨论】:

    标签: c# wpf combobox


    【解决方案1】:

    如果你想像这样绑定ItemsSource="{Binding WonderList}",你必须先设置DataContext

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

    Binding 会在 Window1 中找到 WonderList,但前提是它也是一个属性。

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

    下一步:如果您将值分配给私有字段名称,则绑定到属性名称是没有用的。用

    替换你的构造函数
    public Wonder(string name, string path)
    {
        this.Name = name;
        this.Path = path;
    }
    

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

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

    WonderList 也是如此。如果您在构造函数中创建 List 到后期,则可能所有绑定都已解决,您什么也看不到。

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

    【讨论】:

      【解决方案2】:

      你的方法不对。简单地说,您应该有一个持有 ObservableCollection 属性的 Wonders 类,该属性绑定到 ComboBox 的 ItemsSource。你应该阅读 MSDN:

      http://msdn.microsoft.com/en-us/library/ms752347.aspx

      【讨论】:

        猜你喜欢
        • 2022-01-11
        • 2013-05-21
        • 1970-01-01
        • 2016-08-23
        • 2012-02-08
        • 2012-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多