繁体   English   中英

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

[英]How to create a custom property editor for Extended WPF Toolkit PropertyGrid with another (nested) editor in C#?

我有以下通用类:

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

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

在我在这里找到的帮助下我已经走了这么远:

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);
// ...

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

任何想法将不胜感激!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM