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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21099067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:30:53  来源:igfitidea点击:

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

c#wpfpropertygrid

提问by zaantar

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 PropertyGridthat will allow me to edit the IsDirtyproperty through a CheckBoxand the Valueproperty through another nested editor.

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

With help I found hereI'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!

任何想法将不胜感激!

回答by Matt

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.

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