Property Editor or TypeConverter?

WPF Studio, Themes, and Shared Library for WPF Forum

Posted 13 years ago by Pat Maneely - Software Engineering Manager, Cobham Aerospace
Version: 11.1.0545
Avatar
I have a Byte property in the class that is bound to to the propertyGrid. It is however used as a Boolean in that the only valid states are 0(false) and 1(true).

I would like the PropertyGrid to display it as a ComboBox with two Items True and False, and then convert them to 1/0.

What is the best approach to accomplish this?

Comments (3)

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

You could go either way. A TypeConverter can be reused by other parts of .NET, including the WinForms PropertyGrid. But that requires you to include the conversion logic in your "data layer". That's not necessarily a bad thing, just something to be aware of. Also, if this property is on a view-model, then it's not really a "data layer". So including the TypeConverter is fine.

With the property editor, you are keeping the presentation logic in the UI. The downside of the property editor is that it cannot be reused by other controls easily.

I would probably go with the TypeConverter though, as it integrates well in the PropertyGrid and can be reused elsewhere.


Actipro Software Support

Posted 13 years ago by Pat Maneely - Software Engineering Manager, Cobham Aerospace
Avatar
I tried to implement a TypeConverter, but I think I'm not understanding some of the requirements of the PropertyGrid Control.

My intent was to convert (ConvertTo) a Byte to a 'True' or 'False' String that the ProeprtyGrid would display in place of a 0 or 1.

Conversely, the ConvertFrom would convert from a 'True' or 'False' string to a Byte (0 or 1).

As will be obvious to you - That's not what is happening, and I'm sure there is a key concept I'm missing.

I will email you the converter code and it will be clear what my misunderstanding is.
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Please see this forum post.

But basically, you need to change the property editor used in order to utilize TypeConverters defined directly on the property. In your case, you could do this instead of what is shown in the Custom TypeConverters QuickStart and the forum post above:
<propgrid:PropertyGrid.PropertyEditors>
      <propgrid:PropertyEditor PropertyName="CsasTxPowerMode" ValueTemplateKey="{x:Static propgrid:BuiltinEditors.DynamicStringValueTemplateKey}" />
</propgrid:PropertyGrid.PropertyEditors>
or
<propgrid:PropertyGrid.PropertyEditors>
      <propgrid:PropertyEditor PropertyType="{x:Type system:Byte}" ValueTemplateKey="{x:Static propgrid:BuiltinEditors.DynamicStringValueTemplateKey}" />
</propgrid:PropertyGrid.PropertyEditors>
This isn't generally as flexible as changing the default property editor though (as seen in our QuickStart).


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.