通知属性更改的字典 [英] Notify Property Changed on a Dictionary
问题描述
我有一个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}">
座椅
公开为一个属性的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
}
}
从我的窗口的代码中,座椅2B的价值有时会改变,而在这之后,我想通知该属性已更改的用户界面。
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.
}
}
我已经看了,看字典是的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
.
推荐答案
博士。 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屋!