Propertygrid bind dictionary

Source: Internet
Author: User

This article excerpt: http://greatverve.cnblogs.com/archive/2012/02/08/propergrid-Dictionary.html

Propertygrid is directly bound to dictionary to display the data type. to display it as text | value, you need to handle it.
The direct binding is shown as follows:

We want to display the following:

Private Void Form6_load ( Object Sender, eventargs E)
{
Dictionary < Int , String > Dictest = New Dictionary < Int , String > ();
Dictest. Add ( 0 , " Item 1 " );
Dictest. Add ( 3 , " Item 2 " );
Dictest. Add ( 5 , " Item 3 " );
Dictest. Add ( 1 , " Item 4 " );

//Propertygrid1.selectedobject = dictest;

//Idictionary d = new hashtable ();
//D ["hello"] = "world ";
//D ["meaning"] = 42;
//D ["shade"] = color. forestgreen;

Propertygrid1.selectedobject =NewDictionarypropertygridadapter (dictest );
}

ClassDictionarypropertygridadapter: icustomtypedescriptor
{
Idictionary _ dictionary;

Public dictionarypropertygridadapter (idictionary D)
{< br> _ dictionary = D;
}< br> // three of the icustomtypedescriptor methods are never called by the property grid, but we'll stub them out properly anyway:

Public string getcomponentname ()
{< br> return typedescriptor. getcomponentname ( This and true );
}

PublicEventdescriptor getdefaultevent ()
{
ReturnTypedescriptor. 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 ()
{
ReturnTypedescriptor. getevents (This,True);
}

PublicTypeconverter getconverter ()
{
ReturnTypedescriptor. getconverter (This,True);
}

Public ObjectGetpropertyowner (propertydescriptor PD)
{
Return_ Dictionary;
}

PublicAttributecollection getattributes ()
{
ReturnTypedescriptor. getattributes (This,True);
}

Public ObjectGeteditor (type editorbasetype)
{
ReturnTypedescriptor. geteditor (This, Editorbasetype,True);
}

PublicPropertydescriptor 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 NewPropertydescriptorcollection (props );
}

}

class dictionarypropertydescriptor: propertydescriptor
{< br> /// 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 )
{< br> _ dictionary = D;
_ key = key;
}< br> // 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
{< br> Get { return _ dictionary [_ key]. getType () ;}< BR >}< br> // if you knew that all of your values were strings, for example, you coshould just return typeof (string ).

// then we implement setvalue and getvalue:

Public override void setvalue ( Object component, Object value)
{< br> _ dictionary [_ key] = value;
}

Public override Object getvalue ( Object component)
{< br> return _ dictionary [_ key];
}< br> Public override bool isreadonly
{< br> Get { return false ;}< BR >}

Public OverrideType componenttype
{
Get{Return Null;}
}

Public Override BoolCanresetvalue (ObjectComponent)
{
Return False;
}

Public Override VoidResetvalue (ObjectComponent)
{
}

Public Override BoolShouldserializevalue (ObjectComponent)
{
Return False;
}
}

private void propertygrid1_propertyvaluechanged ( Object S, propertyvaluechangedeventargs E)
{< br> MessageBox. show (E. changeditem. label + " , " + E. changeditem. value);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.