TypeConverterAttribute question

Grids for WPF Forum

Posted 14 years ago by EXAKOM
Version: 10.1.0521
Avatar
Hello,
I need to have a Thickness and CornerRadius property in my PropertyGrid.
I have a class with several properties, two of them being a CornerRadius and a Thickness.
By default, the property grid is showing a string like "0,0,0,0". I change it to "1,1,1,1", it s not accepted. I don't want to make a special PropertyEditor for that, I thought I could use a type converter attribute on my properties, but it doesn't seem to do the trick.
Any idea?
Thanks :)

Comments (2)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Nicolas,

The PropertyGrid offers two ways to set the value of an underlying property. The first method uses IPropertyDataAccessor.Value, which is an object and simply sets the value directly. The second method uses IPropertyDataAccessor.ValueAsString, which uses a string value that is converted to the appropriate type.

When using the former property, the WPF binding system performs any conversion needed and doesn't always take the TypeConverter into account. When using the latter property, our code performs the conversion using the associated TypeConverter (which can be defined on the property or the associated type). So if you use the Value property, then the TypeConverter will not be picked up if it's defined on the property.

By default, our built-in property editors access the underlying property using the Value property. Typically you only want to use ValueAsString when you are using a TextBox or editable ComboBox, since things like CheckBox work better with the actual value. We do provide alternate versions of our property editors that access the underlying property using ValueAsString, but you have to hook them up.

You can either switch the editors for just the types CornerRadius and Thickness, or globally. To change the editors just for the specific types, you'd use something like this:
<propgrid:PropertyGrid.PropertyEditors>
    <propgrid:PropertyEditor PropertyType="CornerRadius" ValueTemplateKey="{x:Static propgrid:BuiltinEditors.DynamicStringValueTemplateKey}" />
    <propgrid:PropertyEditor PropertyType="Thickness" ValueTemplateKey="{x:Static propgrid:BuiltinEditors.DynamicStringValueTemplateKey}" />
</propgrid:PropertyGrid.PropertyEditors>
You can change it globally by setting BuiltinEditors.DefaultValueTemplateKey to BuiltinEditors.DynamicStringValueTemplateKey. The default value for BuiltinEditors.DefaultValueTemplateKey is BuiltinEditors.DynamicValueTemplateKey.


Actipro Software Support

Posted 14 years ago by EXAKOM
Avatar
Thanks! It worked :)
The latest build of this product (v24.1.1) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.