如何使用C#中的另一个(嵌套)编辑器为Extended WPF Toolkit PropertyGrid创建自定义属性编辑器?

时间:2014-01-13 18:53:53

标签: c# wpf propertygrid

我有以下通用类:

public class Member<T>
{
    public bool IsDirty { get; set; }
    public T Value { get; set; }
}

我想为PropertyGrid创建一个自定义编辑器,允许我通过IsDirty编辑CheckBox属性,并通过另一个嵌套编辑器编辑Value属性。

在帮助下我发现here我已经走到了这一步:

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;
    }
}

现在我正在寻找一种替换TextBox的方法,如下所示:

// ...
TEditor editorResolver;
PropertyItem innerPropertyItem;
// ... magic happens here ...
FrameworkElement valueEditor = editorResolver.ResolveEditor(innerPropertyItem);
// ...

主要目标是避免为每个嵌套编辑器类型创建新类。

非常感谢任何想法!

1 个答案:

答案 0 :(得分:0)

看看我在SO question中提供的解决方案,我通过一个按钮和一个单独的窗口提供自定义编辑器。