簡體   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