Custom Editor

Grids for WPF Forum

Posted 10 years ago by Naomi Weiser
Avatar
I created an editor with a DataTemplate and ResourceDictionary.
How can I add my editor from code to my property class , like the following which works for your editors?
class...
{
[Editor(typeof(CheckBoxPropertyEditor), typeof(PropertyEditor))]
public bool MyProperty {...}
}

I am able to add my editor as follows:
PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "prop11";
item.Value = 14;
MyEditorResources resources = new MyEditorResources();
item.ValueTemplate = resources.HexEditor;
propertyGrid.Properties.Add(item);

class MyEditorResources:ResourceDictionary
{
public DataTemplate PhoneNumberEditor
{
get { return (DataTemplate)this["PhoneNumberEditor"]; }
}
}

Comments (4)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Naomi,

You would simply need to create a custom PropertyEditor class that returns the appropriate DataTemplate or resource key. This is similar to what we do in CheckBoxPropertyEditor:
public class PhoneNumberPropertyEditor : PropertyEditor {

    /// <summary>
    /// Gets or sets the <see cref="DataTemplate"/> for the value cell.
    /// </summary>
    /// <value>A <see cref="DataTemplate"/>.</value>
    /// <remarks>
    /// The order of precedence for the name template selection is: <see cref="ValueTemplate"/>,
    /// <see cref="ValueTemplateSelector"/>, and finally <see cref="ValueTemplateKey"/>.
    /// </remarks>
    public override DataTemplate ValueTemplate {
        get { return MyEditorResource.PhoneNumberEditor; }
        set { /* No-op */ }
    }

    /// <summary>
    /// This property is not used by this class.
    /// </summary>
    /// <value><see langword="null"/>.</value>
    public override ResourceKey ValueTemplateKey {
        get { return null; }
        set { /* No-op */ }
    }

    /// <summary>
    /// This property is not used by this class.
    /// </summary>
    /// <value><see langword="null"/>.</value>
    public override DataTemplateSelector ValueTemplateSelector {
        get { return null; }
        set { /* No-op */ }
    }
}
Then you would use your custom class just like the CheckBoxPropertyEditor.


Actipro Software Support

Posted 10 years ago by Naomi Weiser
Avatar
thanks, worked great!
One more question, when i do this, I now see the property with the editor, however when i add the ediotr my get/set dont work, i.e typing text into the textbox doesn't update the value of the property in my class, i have tried different binding methods in my datatemplate, but none are workingm,what am i missing?

class PropObject :INotifyPropertyChanged{
[Editor(typeof(PhoneEditor), typeof(PropertyEditor))]
public string MyPhone
{get{return this.myPhone;}
set{this.myPhone = value;
this.OnPropertyChanged("MyPhone");}}}

<ResourceDictionary ....
<DataTemplate x:Key="PhoneEditor">
<Grid><TextBox Text="{Binding StringValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="{Binding IsReadOnly}"/></Grid>
</DataTemplate>
...>

[Modified at 03/18/2009 05:09 AM]
Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Naomi,

I'd recommend looking at the Property Editors QuickStart in our Sample Browser. But to answer your question, you need to do something like this:
<TextBox Text="{Binding Value, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
    IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource AncestorType={x:Type propgrid:IPropertyDataAccessor}}}" />


Actipro Software Support

Posted 10 years ago by Naomi Weiser
Avatar
thanks!
The latest build of this product (v2018.1 build 0675) was released 14 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.