修改ItemsSource ObservableCollection后,如何刷新一个组合框 [英] How can I refresh a combobox after modifying its ItemsSource ObservableCollection

查看:685
本文介绍了修改ItemsSource ObservableCollection后,如何刷新一个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题很简单:当 ItemsSource 更新时,组合框不会刷新新项目似乎没有被添加到组合框中的项目列表中。

The problems is simple: when ItemsSource is updated Combobox doesn't "refresh" e.g. new items don't appear to be added to the list of items in the combobox.

我已经尝试过这个问题的答案: WPF - 自动刷新组合框内容,没有运气。

I've tried the solution from aceepted answer to this question: WPF - Auto refresh combobox content with no luck.

这里是我的代码,
XAML:

here's my code, XAML:

<ComboBox Name="LeadTypeComboBox" ItemsSource="{Binding LeadTypeCollection}" />

ViewModel:

ViewModel:

public ObservableCollection<XmlNode> LeadTypeCollection { get; set; }

我更新此集合的方式是单独的方法,它从更新的XML文件中加载数据: this.LeadTypeCollection = GetLeadTypesDataSource();

the way I update this collection is in the separate method, which loads data from updated XML file: this.LeadTypeCollection = GetLeadTypesDataSource();

我还尝试使用添加用于测试目的:

I've also tried using Add for testing purposes:

this.LeadTypeCollection = GetLeadTypesDataSource();
ItemToAdd = LeadTypeCollection[LeadTypeCollection.Count - 1];
this.LeadTypeCollection.Add(ItemToAdd);

代码更新集合肯定启动,调试时我可以看到这个集合中的新项目,但是在组合框中看不到它们。

the code updating collection definitely kicks off, I can see new items in this collection when debugging, but I don't see them in the combobox.

在xaml代码隐藏中执行此操作: LeadTypeComboBox.ItemsSource = MyViewModel.GetLeadTypesDataSource(); 但是我想使用MVVM实现这一点,即代码必须在ViewModel中,不知道LeadTypeComboBox控件。

Doing this in the xaml code-behind works: LeadTypeComboBox.ItemsSource = MyViewModel.GetLeadTypesDataSource(); but I'd like to achieve this with MVVM, i.e. the code must be in ViewModel which isn't aware of LeadTypeComboBox control.

推荐答案

我想我以前看过这个,解决方案是更新收集属性来提高变化。

I think I have seen this before and the solution was to update the collection property to raise the change.

ie

public class MyViewModel : INotifyPropertyChanged
{
    private ObservableCollection<XmlNode> leadTypeCollection;

    public string LeadTypeCollection
    { 
        get { return leadTypeCollection; }
        set
        {
            if (value != leadTypeCollection)
            {
                leadTypeCollection = value;
                NotifyPropertyChanged("LeadTypeCollection");
            }
        }

    public MyViewModel()
    {
        leadTypeCollection = new ObservableCollection<XmlNode>();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        PropertyChanged.Raise(this, info);
    }
}

我有一个扩展方法来提高属性(如public static void Raise(此PropertyChangedEventHandler处理程序,对象发件人,字符串属性名称)
{b

I have an extension method for raising the property (as found elsewhere on stackoverflow):

public static void Raise(this PropertyChangedEventHandler handler, object sender, string propertyName)
{
    if (null != handler)
    {
        handler(sender, new PropertyChangedEventArgs(propertyName));
    }
}

这篇关于修改ItemsSource ObservableCollection后,如何刷新一个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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