How do you set a generic property editor

Grids for WPF Forum

Posted 14 years ago by Richard Carlin
Avatar
I have a property grid that is bound to one of my data sources. It currently displays all the color properties in a text box. Is there an easy way in xaml or C# to to tell the property grid to use the SpectrumColorPicker whenever it sees any property of type Color.

Comments (7)

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

The PropertyEditor class can be used to map a property (by name, type, or both) to a specific DataTemplate (which contains the actual controls used to edit the value). You can add your custom PropertyEditor to the BuiltinEditors.PropertyEditors collection or to an specific PropertyGrid.PropertyEditors collection (the former is used globally by all PropertyGrids and the latter is used by a single PropertyGrid instance).

The "Actipro PropertyGrid\User Interface Layer\Property Editors" topic in our help file discusses this, and our sample application has some examples (just search for "PropertyEditors").


Actipro Software Support

Posted 14 years ago by Richard Carlin
Avatar
I cant seem to find a way to map the SpectrumColorPicker to type Color.

The help files tell how to map a TextBox, ComboBox, CheckBox, RadioButtonList to a type but not other editors. The help file also shows how to use a property level definition but the use of SpectrumColorPicker is not understood.

The sample application shows how to define each property field and map the SpectrumColorPicker to it but I am not defining each field, I am using a SelectedObject with properties.

Thinking out of the box I tried changing the property from Media.Color to Drawing.Color to see what happens. The editor defaults to a combobox with a list of standard colors which is better than a textbox but when you change the combobox selection to a new color all you get an a red box around the editor because although it can read a Drawing.Color it cant change it.

Please help because right now the property grid type color defaults to a textbox and none of my users have any idea what a hex color is, they just want to choose a color.

[Modified at 12/10/2009 08:45 AM]
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Richard,

You are looking at the built-in editors that use the TextBox, ComboBox, etc controls. What you can do is define a DataTemplate that uses a SpectrumColorPicker. Then you can add an instance of PropertyEditor, whose PropertyType is set to typeof(Color) and ValueTemplate is set to your custom DataTemplate, to Builtin.PropertyEditors or PropertyGrid.PropertyEditors.

So something like this:
<Window ...
    xmlns:propgrid="http://schemas.actiprosoftware.com/winfx/xaml/propgrid"
    xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared">
    <Window.Resources>
        <DataTemplate x:Name="ColorValueTemplate">
            <shared:SpectrumColorPicker Margin="0" HorizontalAlignment="Left" VerticalAlignment="Center"
                    Width="100" Height="75"
                    InitialColor="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=OneTime}"
                    SelectedColor="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=TwoWay}" />
        </DataTemplate>
    </Window.Resources>
    
    <propgrid:PropertyGrid ...>
        <propgrid:PropertyGrid.PropertyEditors>
            <propgrid:PropertyEditor PropertyType="{x:Type Color}" ValueTemplate="{StaticResource ColorValueTemplate}" />
        </propgrid:PropertyGrid.PropertyEditors>
    </propgrid:PropertyGrid>
</Window>
In addition, you may want to look at our Editors for WPF product. It has a Color editor control, which includes integration into the PropertyGrid. The Sample Browser shows examples of these.

Hope this helps.


Actipro Software Support

Posted 14 years ago by Richard Carlin
Avatar
Thanks, it almost works. I'm not sure what is going on but when I attach my data structure to SelectedObject, the SpectumColorPicker does not show the initial or selected color but when I change the color the property gets changed to the correct color. Then when I relink it to Selected object the SpectumColorPicker shows the wrong color again.

I also tried the ColorEditBox which I like better. It shows up in the property grid perfectly but every time I click the expansion combobox like arrow the program blows up everytime with "Cannot perform this operation while dispatcher processing is suspended". I used the following for the ColorEditBox

<DataTemplate x:Key="ColorValueTemplate">
<Editors1:ColorEditBox Margin="0"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Value="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type Primitives:IPropertyDataAccessor}}, Mode=TwoWay}">
</Editors1:ColorEditBox>
</DataTemplate>
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Richard,

Can you please put together a small sample project that reproduces the issue(s) you are seeing and email it over to our support address? The above code works fine for us, so it may have something to do with other parts of your code.

Thanks


Actipro Software Support

Posted 14 years ago by Richard Carlin
Avatar
I have this figured out now after working with a simpler sample. The first error with the color had to do with a property translation error. The blowup had to do with a problem in code triggered by the property update event.

Everything is working well now. Thanks.
Posted 14 years ago by Richard Carlin
Avatar
Just a note. The problem event was because I was using SelectedItemChanged to tell the system a property had changed since there isn't a PropertyChanged attached to the Property Grid. The SelectedItemChanged was triggered when you tried to open the ColorPicker. My data structure has a complicated tree structure and I can end up attaching up to 100 or more property changed events. I was trying to find a simple way to attach a single event handler. To fix it I have attached all the property update events and removed the SelectedItemChanged.

I have seen posts that adding a Property Changed Event to the Property Grid is on the list of new features but I would like to add my vote for it.
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.