Custom TypeConverters and PropertyGridPropertyItem

Grids for WPF Forum

Posted 15 years ago by wpf.acp
Version: 9.1.0505
Avatar
Hi,

I believe I have found a problem when PropertyGrid is constructed using a series of PropertyGridPropertyItem instead of using SelectedObject property on a PropertyGrid. In the latter case a TypeConverter is correctly used when displaying/changing properties in a PropertyGrid. If a PropertyGrid is modified to use a series of PropertyGridPropertyItem entries, TypeConverters are never used.

To replicate the issue, please open MainControl.xaml of "Custom TypeConverters QuickStart" of your own Sample Browser and simply copy the PropertyGrid found there to another Grid column. Then modify one of the ProperyGrids not to use SelectedObject but instead specify a single PropertyGridPropertyItem like so:
<propgrid:PropertyGridPropertyItem ValueName="MyDouble" Value="{Binding Source={StaticResource TestObject},Path=MyDoubleWith}" />
Build and run the app. Please notice how changing the value of MyDouble in any of the PropertyGrids is updated in the other but also please notice that the modified PropertyGrid neither uses a TypeConverter to format nor parses the value as temperature (just a plain double).

Is there some documented way to achieve the same functionality when using a PropertyGridPropertyItem or is this really a bug? Can one explicitly specify a TypeConverter on a PropertyGridPropertyItem? I have noticed that there is a read-only Converter property on IPropertyDataAccessor.

Alternatively, is there an attribute for tagging a class property that a PropertyGrid understands and omits a particular property when using a SelectedObject?

Thanks!

Comments (3)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

TypeConverters defined on properties are not supported when using PropertyGridPropertyItem. The TypeConverter needs to be defined on the type (class, struct, etc). PropertyGridPropertyItem doesn't really have access to the property, it's just given a value via the Binding. We can look into using reflection to get the TypeConverter from the associated binding expression, but there are several complications with that approach.

The only work around would be to build a custom property editor, and use an IValueConverter that performs the same "conversion" on the TextBox.Text property binding. Alternatively, you could probably create a derivation of PropertyGridPropertyItem and override IPropertyDataAccessor.Converter.
/// <summary>
/// Gets the <see cref="TypeConverter"/> to use for the associated property.
/// </summary>
/// <value>
/// The <see cref="TypeConverter"/> to use for the associated property.
/// </value>
TypeConverter IPropertyDataAccessor.Converter {
    get {
        Converter<PropertyGridPropertyItem, TypeConverter> action = delegate(PropertyGridPropertyItem p) {
            return new MyConverter();
        };

        if (this.Dispatcher.CheckAccess())
            return action(this);
        else
            return (TypeConverter)this.Dispatcher.Invoke(DispatcherPriority.Normal, action, this);
    }
}
You can hide properties by setting the BrowsableAttribute to false (ie. add [Browsable(false)]). Or you can use the PropertyGrid.DataFilter to filter any property.


Actipro Software Support

Posted 15 years ago by wpf.acp
Avatar
I agree that given just a binding it would probably be hard to guess the correct TypeConverter but inspired by your suggestion it didn't take me very much time to derive PropertyGridPropertyItem with a setter for Converter property. This has given me the opportunity to specify a TypeConverter for a particular PropertyGridPropertyItem just as I wanted.

I wonder why there is no built-in setter for Converter property? Will you consider adding this or is there a strong reason for not doing so?

Thanks a lot!
Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
We may be able to add a setter. I've marked down a TODO item to see about adding that.

Typically, the TypeConverter does not change. When using SelectObject(s), the TypeConverter of the resulting PropertyDescriptor/PropertyInfo can't really be changed (it could but changes would not be picked up until the PropertyGrid is refreshed).

PropertyGridPropertyItem was mostly designed to work like the PropertyDescriptor/PropertyInfo. But it does seem like a nice feature, but we just need to verify that changes would be immediately picked up (which the underlying code may not support).


Actipro Software Support

The latest build of this product (v24.1.2) was released 10 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.