通知字典上的属性已更改 [英] Notify Property Changed on a Dictionary

查看:11
本文介绍了通知字典上的属性已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WPF/XAML 表单数据绑定到字典中的属性,类似于:

I have a WPF / XAML form data-bound to a property in a dictionary, similar to this:

<TextBox Text="{Binding Path=Seat[2B].Name}">

Seat 作为飞机对象上的属性 IDictionary 公开.

Seat is exposed as a property IDictionary<String, Reservation> on the airplane object.

class airplane
{
    private IDictionary<String, Reservation> seats;
    public IDictionary<String, Reservation> Seat
    {
        get { return seats; }
        // set is not allowed
    }
}

在我的Window的代码中,seat 2B的值有时会发生变化,然后我想通知UI该属性发生了变化.

From within the code of my Window, the value of seat 2B is sometimes changed, and after that, I want to notify the UI that the property has changed.

class MyWindow : Window
{
    private void AddReservation_Click(object sender, EventArgs e)
    {
        airplane.Seat["2B"] = new Reservation();
        // I want to override the assignment operator (=)
        // of the Seat-dictionary, so that the airplane will call OnNotifyPropertyChanged.
    }
}

我查看了 Dictionary 是否是 IObservable,以便我可以观察它的变化,但似乎不是.

I've looked to see if the Dictionary is IObservable, so that I could observe changes to it, but it doesn't seem to be.

有什么好的方法可以捕捉"飞机类中字典的变化,以便我可以NotifyPropertyChanged.

Is there any good way to "catch" changes to the dictionary in the airplane-class so that I can NotifyPropertyChanged.

推荐答案

Dr.WPF 在此链接上创建了一个 ObservableDictionary:http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/

Dr. WPF has created an ObservableDictionary at this link: http://drwpf.com/blog/2007/09/16/can-i-bind-my-itemscontrol-to-a-dictionary/

更新: WPF 博士在以下链接中的评论说他已经自己解决了这个问题,因此不再需要以下更改

此外,在此链接中添加了:http://10rem.net/blog/2010/03/08/binding-to-a-dictionary-in-wpf-and-silverlight

Also, an addition was made at this link: http://10rem.net/blog/2010/03/08/binding-to-a-dictionary-in-wpf-and-silverlight

小的变化是

// old version
public TValue this[TKey key]
{
    get { return (TValue)_keyedEntryCollection[key].Value; }
    set { DoSetEntry(key, value);}
}

// new version
public TValue this[TKey key]
{
    get { return (TValue)_keyedEntryCollection[key].Value; }
    set
    {
        DoSetEntry(key, value);
        OnPropertyChanged(Binding.IndexerName);
    }
}

这篇关于通知字典上的属性已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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