一般观测Dictionary类的数据绑定/ WPF C# [英] General Observable Dictionary Class for DataBinding/WPF C#

查看:855
本文介绍了一般观测Dictionary类的数据绑定/ WPF C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#中创建一个观测Dictionary类为WPF数据绑定。
我发现安迪在这里一个很好的例子:<一href=\"http://stackoverflow.com/questions/800130/two-way-data-binding-with-a-dictionary-in-wpf/800217\">http://stackoverflow.com/questions/800130/two-way-data-binding-with-a-dictionary-in-wpf/800217

I'm trying to create a Observable Dictionary Class for WPF DataBinding in C#. I found a nice example from Andy here: http://stackoverflow.com/questions/800130/two-way-data-binding-with-a-dictionary-in-wpf/800217

据,我试图将code更改为以下内容:

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?

感谢您!

干杯

推荐答案

如果你真的想打一个 ObservableDictionary ,我建议创建一个实现这两个类的IDictionary INotifyCollectionChanged 。您可以随时使用词典内部实施的IDictionary 的方法,这样你就不必重新实现自己

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.

既然你有当内部词典的变化,你可以使用这些知识来实施 INotifyCollectionChanged 全部知识。

Since you have full knowledge of when the internal Dictionary changes, you can use that knowledge to implement INotifyCollectionChanged.

这篇关于一般观测Dictionary类的数据绑定/ WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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