我有以下通用类:
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);
// ...
主要目标是避免为每个嵌套编辑器类型创建新类。
非常感谢任何想法!