Setting the Color editor for a string field

Grids for WPF Forum

Posted 6 years ago by Hagay
Version: 17.2.0662
Avatar

Hi,

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:PropertyGrid.PropertyEditors>
        <grids:PropertyEditor PropertyName="Color">
            <grids:PropertyEditor.ValueTemplate>
                <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}"
                                          IsNullAllowed="True"
                                          PlaceholderText="{Binding ValuePropertyEditor.PlaceholderText}"
                                          SpinnerVisibility="{Binding ValuePropertyEditor.SpinnerVisibility}"
                                          SpinWrapping="{Binding ValuePropertyEditor.SpinWrapping}"
                                          DefaultValue="{Binding ValuePropertyEditor.DefaultValue}"
                                          HasSwatch="{Binding ValuePropertyEditor.HasSwatch}"
                                          IsAlphaEnabled="{Binding ValuePropertyEditor.IsAlphaEnabled}"/>
                </DataTemplate>
            </grids:PropertyEditor.ValueTemplate>
        </grids:PropertyEditor>
    </grids:PropertyGrid.PropertyEditors>
</grids:PropertyGrid>

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

 So... is there?

Comments (6)

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

Hello,

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 6 years ago by Hagay
Avatar

Thank You!

This is how it works now -

<grids:PropertyGrid.PropertyEditors>
    <gridseditors:ColorPropertyEditor PropertyName="Color" PropertyType="system:String" />
</grids:PropertyGrid.PropertyEditors>
Posted 6 years ago by Hagay
Avatar

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 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

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 6 years ago by Hagay
Avatar

Hi,

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 6 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

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

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.