数据绑定/ WPF的通用可见字典类C# [英] General Observable Dictionary Class for DataBinding/WPF C#
问题描述
我正在C#中创建WPF DataBinding的Observable Dictionary类。
我从Andy发现了一个很好的例子: WPF中使用字典的双向数据绑定
据此,我尝试将代码更改为以下内容:
class ObservableDictionary:ViewModelBase
{
public ObservableDictionary(Dictionary< TKey,TValue>字典)
{
_data =字典;
}
私人字典< TKey,TValue> _数据;
public Dictionary< TKey,TValue>数据
{
get {return this._data; }
}
私钥KeyValuePair< TKey,TValue>? _selectedKey = null;
public KeyValuePair< TKey,TValue>? SelectedKey
{
get {return _selectedKey; }
set
{
_selectedKey = value;
RaisePropertyChanged(SelectedKey);
RaisePropertyChanged(SelectedValue);
}
}
public TValue SelectedValue
{
get
{
return _data [SelectedKey.Value.Key];
}
set
{
_data [SelectedKey.Value.Key] = value;
RaisePropertyChanged(SelectedValue);
}
}
}
}
不幸的是,我还是不知道如何传递一般字典对象..任何想法?
谢谢! p>
干杯
如果你真的想做一个 ObservableDictionary
,我建议创建一个实现 IDictionary
和 INotifyCollectionChanged
。您可以随时在内部使用字典
来实现 IDictionary
的方法,以便您不必重新实现自己
由于您完全了解内部 Dictionary
何时更改,您可以使用该知识来实现 INotifyCollectionChanged
。
I'm trying to create a Observable Dictionary Class for WPF DataBinding in C#. I found a nice example from Andy here: Two Way Data Binding With a Dictionary in WPF
According to that, I tried to change the code to following:
class ObservableDictionary : ViewModelBase
{
public ObservableDictionary(Dictionary<TKey, TValue> dictionary)
{
_data = dictionary;
}
private Dictionary<TKey, TValue> _data;
public Dictionary<TKey, TValue> Data
{
get { return this._data; }
}
private KeyValuePair<TKey, TValue>? _selectedKey = null;
public KeyValuePair<TKey, TValue>? SelectedKey
{
get { return _selectedKey; }
set
{
_selectedKey = value;
RaisePropertyChanged("SelectedKey");
RaisePropertyChanged("SelectedValue");
}
}
public TValue SelectedValue
{
get
{
return _data[SelectedKey.Value.Key];
}
set
{
_data[SelectedKey.Value.Key] = value;
RaisePropertyChanged("SelectedValue");
}
}
}
}
Unfortunately I still don't know how to pass "general" Dictionary Objects.. any ideas?
Thank you!
Cheers
If you really want to make an ObservableDictionary
, I'd suggest creating a class that implements both IDictionary
and INotifyCollectionChanged
. You can always use a Dictionary
internally to implement the methods of IDictionary
so that you won't have to reimplement that yourself.
Since you have full knowledge of when the internal Dictionary
changes, you can use that knowledge to implement INotifyCollectionChanged
.
这篇关于数据绑定/ WPF的通用可见字典类C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!