Setting the Color editor for a string field

Posted 5 months ago by Avatar Hagay


I would like to set the ColorPropertyEditor (or NullableColorPropertyEditor) for some string property named "Color".

I managed to accomplish that by the following:

<grids:PropertyGrid IsSummaryVisible="False" 
                    DataObject="{Binding Path=ActivitySettings}">
        <grids:PropertyEditor PropertyName="Color">
                <DataTemplate DataType="grids:IPropertyModel">
                    <editors:ColorEditBox BorderThickness="0" 
                                          Value="{Binding ValueAsString, Mode=TwoWay}" 
                                          IsReadOnly="{Binding IsReadOnly}"
                                          CommitTriggers="{Binding ValuePropertyEditor.CommitTriggers}"
                                          HasPopup="{Binding ValuePropertyEditor.HasPopup}"
                                          IsArrowKeyPartNavigationEnabled="{Binding ValuePropertyEditor.IsArrowKeyPartNavigationEnabled}"
                                          IsEditable="{Binding ValuePropertyEditor.IsEditable}"
                                          PlaceholderText="{Binding ValuePropertyEditor.PlaceholderText}"
                                          SpinnerVisibility="{Binding ValuePropertyEditor.SpinnerVisibility}"
                                          SpinWrapping="{Binding ValuePropertyEditor.SpinWrapping}"
                                          DefaultValue="{Binding ValuePropertyEditor.DefaultValue}"
                                          HasSwatch="{Binding ValuePropertyEditor.HasSwatch}"
                                          IsAlphaEnabled="{Binding ValuePropertyEditor.IsAlphaEnabled}"/>

 However, I wonder if there's a better more elegant way.

 So... is there?

Comments (6)

Posted 5 months ago by Actipro Software Support - Cleveland, OH, USA


Yes please see the "Editors / Interoperability / PropertyGrid" topic in the documentation that comes with the product.  That tells you how to use one line to register all our edit boxes as editors, and also how to alternately use pre-built property editors like ColorPropertyEditor.

Actipro Software Support
Posted 5 months ago by Hagay

Thank You!

This is how it works now -

    <gridseditors:ColorPropertyEditor PropertyName="Color" PropertyType="system:String" />
Posted 5 months ago by Hagay

Oh, I beg your pardone, but what I wrote above doesn't work.

I'm having an ArgumentException when the editor tries to set the String property with a Color value.

The get works ok, i.e. the editor correctly displays the selected Color according to the String value.

Posted 5 months ago by Actipro Software Support - Cleveland, OH, USA


The ColorEditBox in ColorPropertyEditor's template is getting/setting a Color-based value.  So it's going to return a Color value on set.  I'm not sure where it's getting the exception for you but you would be better off switching your property to be of type Color instead.  Then you won't have any problems.

Actipro Software Support
Posted 5 months ago by Hagay


It doesn't make sense.
I would be convinced with your suggestion if not the Setter nor the Getter would work, but that's not true.
The editor successfully displays the pre-defined value of the property.
Why wouldn't it be able to also set it?
Not to say the Color always has unique String representation.

Also, the snippet I put in my question works just fine.
Note that it is exactly how the editor is defined in your library, only binding to ValueAsString rather than to Value.

Posted 5 months ago by Actipro Software Support - Cleveland, OH, USA


Our ColorPropertyEditor is binding its ColorEditBox.Value to the property descriptor's Value property, since it expects its usage to be tied to a Color-based property value.  If you would need to bind the ColorEditBox to a string-based property instead (like ValueAsString), then you would probably need to make a DataTemplate (similar to what you originally posted) and update it such that it included an IValueConverter in your binding that could convert from strings to Color and back.  Otherwise you are probably running into mismatched types.

Note that you can move your DataTemplate into App.Resources, give it a x:Key and reference it via a StaticResource anywhere you need it.  That might at least make your window XAML a bit cleaner.

Actipro Software Support
Information The latest build of this product (2018.1 build 0670) was released 7 days ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.