abe*_*nky 5 c# data-binding wpf
我有一个WPF/XAML表单数据绑定到字典中的属性,类似于:
<TextBox Text="{Binding Path=Seat[2B].Name}">
Run Code Online (Sandbox Code Playgroud)
Seat暴露为IDictionary<String, Reservation>飞机物体上的属性.
class airplane
{
private IDictionary<String, Reservation> seats;
public IDictionary<String, Reservation> Seat
{
get { return seats; }
// set is not allowed
}
}
Run Code Online (Sandbox Code Playgroud)
从我的窗口代码中,有时会更改座位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.
}
}
Run Code Online (Sandbox Code Playgroud)
我看看是否有词典IObservable,所以我可以观察它的变化,但它似乎不是.
是否有任何好方法可以"抓住"飞机级字典中的更改,以便我可以NotifyPropertyChanged.
Fre*_*lad 13
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19271 次 |
| 最近记录: |