Using EditorAttribute to specify Custom Editor

Grids for WPF Forum

Posted 13 years ago by Greg Horvath
Version: 11.1.0542
Avatar
Hello,

I am evaluating the PropertyGrid control and have a question about setting custom edtitors through the EditorAttribute on a data class.

I have a standard .NET class with a series of properties. Instances of this class get set to the PropertyGrid.SelectedObject. In the example below I am trying to use the DialogTextBoxPropertyEditor to provide a larger textbox for editing the notes field, but when the PropertyGrid is displayed the button to open the dialog is disabled. How do I get this editor to work. Also are there some good examples of how to create my own custom editors which can be associated to properties using the EditorAttribute. Thanks.

public class DataClass
{
        [Description("Notes")]
        [Category("Definition")]
        [Editor(typeof(PropertyEditors.DialogTextBoxPropertyEditor), typeof(PropertyEditors.PropertyEditor))]
        public string Notes
        {
            get { return _notes; }
            set { _notes = value; RaisePropertyChanged(() => Notes); }
        }
}

Comments (5)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Greg,

Please see the Property Editors QuickStart, as that shows how to add a handler for the PropertyGrid.ShowPropertyDialogCommand (which is used by that button). You need to handle that routed command to enable/disable the button and to show your dialog when it's clicked.

The Editors Integration (Custom) (which is actually located under the Editors product) contains several examples of custom PropertyEditors, which can be used with the EditorAttribute. But effectively you define the actually controls using a DataTemplate. The PropertyEditor just maps one or more property to that DataTemplate.


Actipro Software Support

Posted 13 years ago by Greg Horvath
Avatar
There is not way to build your own custom editors similar to the ones built in the Editors.Interop assembly which can be bound to a property through the EditorAttribute. I am building a data driven framework where users can build thier own components that are edited in the property grid. I have no way of knowing in advance what types of properties I have to support so I cannot just build a datatemplate into my UI. It seems like it is to build custom editors that can be attached to propertiese because you have done it with the interop assemblies, any way you could provide an example of how to build our own? Thanks.
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Greg,

The Editors Integration (Custom) demo does show how the Editors/PropertyGrid Interop property editors are defined. The Interop uses fixed DataTemplates, which are referenced by custom classes that derive from PropertyEditor. Then those PropertyEditor classes are assigned to properties using EditorAttribute.

In addition, those PropertyEditor classes can be added to PropertyGrid.PropertyEditors to provide a more dynamically way of mapping properties to property editors. The EditorAttribute is typically defined at compile-time, but this isn't necessarily required. The PropertyGrid.PropertyEditors is a collection, which can easily be modified at runtime.

You would have to know the types of the properties in order to build property editors for them. Whether the property editors are part of your application or loaded via an end-user plug-in, doesn't really matter to the PropertyGrid. But you would have to map the property editors to the properties at some point. And the property editors must provide a DataTemplate.

You can dynamically build up a DataTemplate using FrameworkElementFactory and return that from your property editor, but again you'd need to know the types of the properties that you want to use that property editor in order to properly map it.


Actipro Software Support

Posted 13 years ago by Greg Horvath
Avatar
Is there an example of how the data template is suppose to assign its selected value back to the selected property? Is this done through databinding?

-Greg
Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Greg,

Yes, the Editors Integration (Custom) demo shows how the Binding needs to be set up. The following DataTemplate is from that sample. As you can see the Binding ties the editor to the associated property value (through our abstraction layer):
<DataTemplate x:Key="{x:Static sample:PropertyEditors.RectValueTemplateKey}">
    <editors:RectEditBox x:Name="editBox" Margin="0" Padding="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
            BorderThickness="0" Background="Transparent" CheckBoxVisibility="Visible" SpinnerVisibility="Visible"
            SpinnerInactiveVisibility="Collapsed" InitialValue="0, 0, 100, 100" NullContent="Size to fit"
            Value="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=TwoWay, ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}" />
    <DataTemplate.Triggers>
        <DataTrigger
                Binding="{Binding IsReadOnly, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}}"
                Value="true">
            <Setter TargetName="editBox" Property="IsEnabled" Value="false" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=(propgrid:PropertyGrid.IsReadOnly), RelativeSource={RelativeSource Self}}"
                Value="true">
            <Setter TargetName="editBox" Property="IsEnabled" Value="false" />
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>
You can also access the property via the ValueAsString property, which is capable of leveraging any TypeConverters defined directly on the property.


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.