加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 通知属性更改字典

发布时间:2020-12-15 19:50:05 所属栏目:百科 来源:网络整理
导读:我有一个 WPF / XAML表单数据绑定到字典中的属性,类似于: TextBox Text="{Binding Path=Seat[2B].Name}" Seat作为属性IDictionary String,Reservation公开.在飞机上. class airplane{ private IDictionaryString,Reservation seats; public IDictionaryStri
我有一个 WPF / XAML表单数据绑定到字典中的属性,类似于:

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

Seat作为属性IDictionary< String,Reservation>公开.在飞机上.

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

从我的窗口代码中,有时会更改座位2B的值,之后,我想通知UI该属性已更改.

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,所以我可以观察它的变化,但它似乎不是.

有没有什么好方法可以“抓住”飞机类中字典的更改,以便我可以NotifyPropertyChanged.

解决方法

WPF博士在此链接上创建了一个ObservableDictionary: 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

微小的变化是

// 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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读