I have following generic class:
public class Member<T>
{
public bool IsDirty { get; set; }
public T Value { get; set; }
}
I want to create a custom editor for the PropertyGrid that will allow me to edit the IsDirty
property through a CheckBox
and the Value
property through another nested editor.
With help I found here I've got this far:
class MemberEditor<T, TEditor> : ITypeEditor where TEditor : ITypeEditor
{
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
//var member = propertyItem.Value as Member<T>;
// checkbox for the Member.IsDirty value
var isDirtyCheckbox = new CheckBox();
var isDirtyBinding = new Binding("Value.IsDirty");
isDirtyBinding.Source = propertyItem;
isDirtyBinding.ValidatesOnDataErrors = true;
isDirtyBinding.ValidatesOnExceptions = true;
isDirtyBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(isDirtyCheckbox, CheckBox.IsCheckedProperty, isDirtyBinding);
// inner editor
var valueEditor = new TextBox();
var valueBinding = new Binding("Value.Value");
valueBinding.Source = propertyItem;
valueBinding.ValidatesOnExceptions = true;
valueBinding.ValidatesOnDataErrors = true;
valueBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(valueEditor, TextBox.TextProperty, valueBinding);
// compose the editor
var dockPanel = new DockPanel();
DockPanel.SetDock(isDirtyCheckbox, Dock.Left);
dockPanel.Children.Add(isDirtyCheckbox);
DockPanel.SetDock(valueEditor, Dock.Right);
dockPanel.Children.Add(valueEditor);
return dockPanel;
}
}
Now I am looking for a way to replace the TextBox, for something like this:
// ...
TEditor editorResolver;
PropertyItem innerPropertyItem;
// ... magic happens here ...
FrameworkElement valueEditor = editorResolver.ResolveEditor(innerPropertyItem);
// ...
The main goal is to avoid creating new class for each nested editor type.
Any ideas will be very much appreciated!