PropertyGrid绑定Dictionary

简介: 本文摘抄:http://greatverve.cnblogs.com/archive/2012/02/08/propergrid-Dictionary.htmlPropertyGrid直接绑定Dictionary显示的是数据类型,若要显示为Text|Value需要处理一下。

本文摘抄:http://greatverve.cnblogs.com/archive/2012/02/08/propergrid-Dictionary.html

PropertyGrid直接绑定Dictionary显示的是数据类型,若要显示为Text|Value需要处理一下。
直接绑定显示如下:

我们希望显示如下:

private void Form6_Load( object sender, EventArgs e)
{
    Dictionary< int, string> dicTest = new Dictionary< int, string>();
    dicTest.Add( 0, " 第一项 ");
    dicTest.Add( 3, " 第二项 ");
    dicTest.Add( 5, " 第三项 ");
    dicTest.Add( 1, " 第四项 ");

    // propertyGrid1.SelectedObject = dicTest;

   
// IDictionary d = new Hashtable();
   
// d["Hello"] = "World";
   
// d["Meaning"] = 42;
   
// d["Shade"] = Color.ForestGreen;

    propertyGrid1.SelectedObject = new DictionaryPropertyGridAdapter(dicTest);
}

class DictionaryPropertyGridAdapter : ICustomTypeDescriptor
{
    IDictionary _dictionary;

    public DictionaryPropertyGridAdapter(IDictionary d)
    {
        _dictionary = d;
    }
    // Three of the ICustomTypeDescriptor methods are never called by the property grid, but we'll stub them out properly anyway:

    public string GetComponentName()
    {
        return TypeDescriptor.GetComponentName( this, true);
    }

    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent( this, true);
    }

    public string GetClassName()
    {
        return TypeDescriptor.GetClassName( this, true);
    }
    // Then there's a whole slew of methods that are called by PropertyGrid, but we don't need to do anything interesting in them:

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents( this, attributes, true);
    }

    EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
    {
        return TypeDescriptor.GetEvents( this, true);
    }

    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter( this, true);
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return _dictionary;
    }

    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes( this, true);
    }

    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor( this, editorBaseType, true);
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return null;
    }

    PropertyDescriptorCollection
        System.ComponentModel.ICustomTypeDescriptor.GetProperties()
    {
        return ((ICustomTypeDescriptor) this).GetProperties( new Attribute[ 0]);
    }
    // Then the interesting bit. We simply iterate over the IDictionary, creating a property descriptor for each entry:

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        ArrayList properties = new ArrayList();
        foreach (DictionaryEntry e in _dictionary)
        {
            properties.Add( new DictionaryPropertyDescriptor(_dictionary, e.Key));
        }

        PropertyDescriptor[] props =
            (PropertyDescriptor[])properties.ToArray( typeof(PropertyDescriptor));

        return new PropertyDescriptorCollection(props);
    }


}

class DictionaryPropertyDescriptor : PropertyDescriptor
{
    // PropertyDescriptor provides 3 constructors. We want the one that takes a string and an array of attributes:

    IDictionary _dictionary;
    object _key;

    internal DictionaryPropertyDescriptor(IDictionary d, object key)
        : base(key.ToString(), null)
    {
        _dictionary = d;
        _key = key;
    }
    // The attributes are used by PropertyGrid to organise the properties into categories, to display help text and so on. We don't bother with any of that at the moment, so we simply pass null.

   
// The first interesting member is the PropertyType property. We just get the object out of the dictionary and ask it:

    public override Type PropertyType
    {
        get { return _dictionary[_key].GetType(); }
    }
    // If you knew that all of your values were strings, for example, you could just return typeof(string).

   
// Then we implement SetValue and GetValue:

    public override void SetValue( object component, object value)
    {
        _dictionary[_key] = value;
    }

    public override object GetValue( object component)
    {
        return _dictionary[_key];
    }
    public override bool IsReadOnly
    {
        get { return false; }
    }

    public override Type ComponentType
    {
        get { return null; }
    }

    public override bool CanResetValue( object component)
    {
        return false;
    }

    public override void ResetValue( object component)
    {
    }

    public override bool ShouldSerializeValue( object component)
    {
        return false;
    }
}

private void propertyGrid1_PropertyValueChanged( object s, PropertyValueChangedEventArgs e)
{
    MessageBox.Show(e.ChangedItem.Label + " , " + e.ChangedItem.Value);
}
目录
相关文章
|
8月前
|
前端开发
WPF-Binding问题-MVVM中IsChecked属性CommandParameter转换值类型空异常
WPF-Binding问题-MVVM中IsChecked属性CommandParameter转换值类型空异常
86 0
|
C#
WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性
原文:WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢? 我向大家介绍一个用 依赖属性(DependencyProperty) 实现的方法。
2437 0
|
C#
WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeView
原文:WPF使用HierarchicalDataTemplate绑定Dictionary生成TreeViewDictionary中的CustomeType是一个集合,将其绑定生成一棵树,树的第一层节点是Dictionary的Key,第二层是CustomeType集合,所有代码用XAML实现。
1404 0
|
C#
WPF通过<x:Array>直接为ListBox的ItemsSource赋值
原文:WPF通过直接为ListBox的ItemsSource赋值 123 123123 111231 ...
1061 0
|
C# 索引
WPF中,多key值绑定问题,一个key绑定一个界面上的对象
原文:WPF中,多key值绑定问题,一个key绑定一个界面上的对象 问题说明: 当用到dictionary来储存数据的时候,有时候需要在界面上绑定一个key来显示value,这时候有两种思路: 一种是写一个自定义的扩展类,类似Binding,这里取名为“MyBinding”,在binding类内部实现key的绑定。
985 0

热门文章

最新文章