From 96a65ec0512345029b1535613db40c1198f5d6eb Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Tue, 4 Oct 2011 18:01:33 +0000 Subject: [PATCH] PropertyGrid: added ability to define editors with a simple attribute called TypeEditorAttirbute. You can use a default editor or create a custom editor as follows: public class MyCustomEditor : ITypeEditor { public FrameworkElement ResolveEditor(PropertyItem propertyItem) { TextBox editor = new TextBox(); Binding binding = new Binding("Value"); //bind to the Value property of the PropertyItem instance binding.Source = propertyItem; binding.Mode = propertyItem.IsWriteable ? BindingMode.TwoWay : BindingMode.OneWay; BindingOperations.SetBinding(editor, TextBox.TextProperty, binding); return editor; } } Then attribute the property to use the editor on: private TimeSpan _timeSpan; [TypeEditor(typeof(MyCustomEditor))] public TimeSpan TimeSpan { get { return _timeSpan; } set { _timeSpan = value; } } --- .../Attributes/TypeEditorAttribute.cs | 18 ++++++++++++++++++ .../Implementation/PropertyGrid.cs | 15 +++++++++++---- .../WPFToolkit.Extended.csproj | 1 + 3 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/TypeEditorAttribute.cs diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/TypeEditorAttribute.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/TypeEditorAttribute.cs new file mode 100644 index 00000000..26bbd28f --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/TypeEditorAttribute.cs @@ -0,0 +1,18 @@ +using System; + +namespace Microsoft.Windows.Controls.PropertyGrid.Attributes +{ + public class TypeEditorAttribute : Attribute + { + public Type Type { get; set; } + + public TypeEditorAttribute(Type type) + { + var valueSourceInterface = type.GetInterface("Microsoft.Windows.Controls.PropertyGrid.Editors.ITypeEditor"); + if (valueSourceInterface == null) + throw new ArgumentException("Type must implement the ITypeEditor interface.", "type"); + + Type = type; + } + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs index c868c7f0..fa2c828b 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs @@ -414,7 +414,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid private FrameworkElement GetTypeEditor(PropertyItem propertyItem) { - //first check for any attribute editor + //first check for an attribute editor FrameworkElement editor = GetAttibuteEditor(propertyItem); //now look for a custom editor based on editor definitions @@ -432,9 +432,16 @@ namespace Microsoft.Windows.Controls.PropertyGrid { FrameworkElement editor = null; - var attribute = GetAttribute(propertyItem.PropertyDescriptor); - if (attribute != null) - editor = new ItemsSourceEditor(attribute).ResolveEditor(propertyItem); + var itemsSourceAttribute = GetAttribute(propertyItem.PropertyDescriptor); + if (itemsSourceAttribute != null) + editor = new ItemsSourceEditor(itemsSourceAttribute).ResolveEditor(propertyItem); + + var editorAttribute = GetAttribute(propertyItem.PropertyDescriptor); + if (editorAttribute != null) + { + var instance = Activator.CreateInstance(editorAttribute.Type); + editor = (instance as ITypeEditor).ResolveEditor(propertyItem); + } return editor; } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj index 282f6c85..81859f16 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj @@ -237,6 +237,7 @@ True +