0

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!

zaantar
  • 1
  • 1
  • 3
  • You should really do WPF UIs in XAML as opposed to procedural C# code. – Federico Berasategui Jan 13 '14 at 19:08
  • Well, I know that. This is just a quickly written sketch based on the example with ITypeEditor. If anyone points towards a solution with XAML, I'll be happy to rewrite it. :-) – zaantar Jan 13 '14 at 19:21

1 Answers1

0

Take a look at the solution that I provided in this SO question, where I provide a custom editor via a button and a separate window.

Community
  • 1
  • 1
Matt
  • 2,682
  • 1
  • 17
  • 24
  • Thank you, but my problem is that I want to avoid creating new editor class for each T in Member. – zaantar Jan 14 '14 at 09:17