Skip to content

bigkoma/DynamicDescriptors

 
 

Repository files navigation

DynamicDescriptors

The WinForms PropertyGrid is really useful, but it isn't particularly easy to customize - especially when binding to objects that are out of your control. DynamicDescriptors helps alleviate this pain by providing a runtime-customizable implementation of ICustomTypeDescriptor that can be bound to the PropertyGrid.

Properties can be customized with an easy-to-use fluent interface:

var instanceToBind = new ExampleClass();

var descriptor = DynamicDescriptor.CreateFromInstance(instanceToBind);

descriptor.GetProperty("PropertyOne") // Get the property using its name.
    .SetDisplayName("Property #1")
    .SetDescription("The first property")
    .SetCategory("Example category");
    
descriptor.GetProperty((ExampleClass x) => x.Property2) // Get the property using an expression.
    .SetDisplayName("Property #2")
    .SetDescription("The second property")
    .SetCategory("Example category");

propertyGrid.SelectedObject = descriptor;

Binding to an object instance

We can create a DynamicDescriptor for an object instance:

var instance = new ExampleClass();

var descriptor = DynamicDescriptor.CreateFromInstance(instance);

Binding to a dictionary

We can create a DynamicDescriptor backed by a dictionary. This will act as if the dictionary key/value pairs are properties of a bound object:

var data = new Dictionary<string, object>();
data["Property1"] = "hello";
data["Property2"] = "world";

var descriptor = DynamicDescriptor.CreateFromDictionary(data);

We can also supply type information:

var data = new Dictionary<string, object>();
data["Property1"] = "value";
data["Property2"] = 1;

var types = new Dictionary<string, Type>();
type["Property1"] = typeof(string);
type["Property2"] = typeof(int);

var descriptor = DynamicDescriptor.CreateFromDictionary(data, types);

What can be customized?

DisplayName:

descriptor.GetProperty("PropertyName").SetDisplayName("Property display name");

This modifies the value returned by the DisplayName property.

Description:

descriptor.GetProperty("PropertyName").SetDescription("A description of the property");

This modifies the value returned by the Description property.

Category:

descriptor.GetProperty("PropertyName").SetCategory("Category name");

This modifies the value returned by the Category property.

Converter:

TypeConverter converter = /* your custom type converter */;
descriptor.GetProperty("PropertyName").SetConverter(converter);

This modifies the value returned by the Converter property.

IsReadOnly:

descriptor.GetProperty("PropertyName").SetReadOnly(true);

This modifies the value returned by the IsReadOnly property.

Property order:

descriptor.GetProperty("PropertyOne").SetPropertyOrder(1);
descriptor.GetProperty("PropertyTwo").SetPropertyOrder(2);
descriptor.GetProperty("PropertyThree").SetPropertyOrder(3);

This modifies the order in which properties are returned by the GetProperties method.

Installation

Just grab it from NuGet

PM> Install-Package DynamicDescriptors

License and copyright

Copyright Matthew King 2012-2016. Distributed under the MIT License. Refer to license.txt for more information.

About

Runtime-customizable ICustomTypeDescriptors.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.7%
  • Other 0.3%