Dynamic fields dialog

Editors for WPF Forum

Posted 13 years ago by Samuel T - Software Engineer, Emerson Process Management
Avatar
Hi,

I have a quick question. I have a generic object.
The properties of the object can only be known at runtime. (# of properties & types)

I'm trying to build a dynamic "edit property fields" dialog. Given that particular object, I would like to have the dialog auto-populate with the corresponding fields (depending on the types of the properties). For example: I want to render a BrushPropertyEditor for typeof(Brush), TextBox for typeof(string) and so on.

From what I understand, there are several options available. EditorAttribute is one of them(directly on properties, or via a custom TypeDescriptor or data factory), but I'm not sure if it's a good idea to carry this EditorAttribute on my data model.

Another that I can think of is to create a DataTemplate/Style which will apply the correct Editor based on the object type.

What would you recommend?

Thanks.

Comments (3)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Samuel,

I'm assuming you meant to post this in the PropertyGrid for WPF forum. There are several places you can specify the associated property editor, but all have pros and cons so we can't really recommend one way over another.

If your generic object includes a collection of properties (i.e. it has a property defined like "List<MyProperty> Properties"), then you'd have to either implement ICustomTypeDescriptor or implement a custom DataFactory. The former is built into .NET, while the latter is specific to our PropertyGrid. The DataFactory is responsible for converting an "object" to a list of IDataAccessors, which can be an IPropertyDataAccessor or ICategoryDataAccessor.

If you have custom objects that implement IPropertyDataAccessor, you can specify the property editor there (via the ValueTemplate property). You can also use the PropertyGrid.PropertyEditors collection to use a predefined mapping, based on object type, property type, or property name. And finally, you can specify a custom DataTemplateSelector (via the PropertyGrid.ValueTemplateSelector) that will select an appropriate DataTemplate.


Actipro Software Support

Posted 13 years ago by Samuel T - Software Engineer, Emerson Process Management
Avatar
Hi,

Actually, I'm not intending this to be a PropertyGrid question. However, it seems to me that the concept could be similar. I'm intending to build a real WPF dialog, which takes in a dynamic object (containing a dictionary of Properties, each with its own definition of data type(brush, location (x,y), string and whatnot). The fields would be populated dynamically, depending on the number of properties, and filled with editor field(s) according to the data type.

I'm just wondering if you've come across this kind of question/problem before and your recommendations would be appreciated. Since this is not a PropertyGrid, custom DataFactory and IPropertyDataAccessor would be out of question.

Thanks.
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Samuel,

I understand now. The BrushPropertyEditor that you referenced in your original question is specific to the PropertyGrid. It hooks into the PropertyGrid's built-in property editor mapping to tie properties of type Brush to the BrushEditBox from our Editors for WPF product. This isn't really something you can leverage outside of the PropertyGrid.

Ultimately, you'd need to have a mapping from property type (any maybe other factors) to an associated DataTemplate. Then you'd probably want to use an ItemsControl and populate it with one or instances of ContentPresenter, whose Content property is set to your item and ContentTemplate is the correct DataTemplate. Then you'd simply build an appropriate DataTemplate, which may or may not include an instance of BrushEditBox.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.