From b3152155b18595f599f9185553a161249a7d8259 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Tue, 4 Oct 2011 16:59:38 +0000 Subject: [PATCH] PropertyGrid: Added new attribute called ItemsSourceAttribute which allows you to use an attribute to specify a custom items source for a ComboBox editor. To use simply create a custom class the implements the IItemsSource interface: public class ColorSource : IItemsSource { public IList GetValues() { return new List() { Colors.Blue, Colors.Gray, Colors.Green }; } } Then decorate your property with the ItemsSource attirbute and specify the Type of source to use: private Color _color = Colors.Green; [ItemsSource(typeof(ColorSource))] public Color Color { get { return _color; } set { _color = value; } } When the PropertyGrid loads, the editor for the Color property will be a ComboBox with an ItemsSource of ColorSource.GetValues(). --- .../Implementation/Attributes/IItemsSource.cs | 10 ++++++ .../Attributes/ItemsSourceAttribute.cs | 18 ++++++++++ .../Editors/ItemsSourceEditor.cs | 24 +++++++++++++ .../Implementation/PropertyGrid.cs | 34 +++++++++++++++++-- .../WPFToolkit.Extended.csproj | 3 ++ 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/ItemsSourceAttribute.cs create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs new file mode 100644 index 00000000..bc8ac08b --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace Microsoft.Windows.Controls.PropertyGrid.Attributes +{ + public interface IItemsSource + { + IList GetValues(); + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/ItemsSourceAttribute.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/ItemsSourceAttribute.cs new file mode 100644 index 00000000..1aff8157 --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/ItemsSourceAttribute.cs @@ -0,0 +1,18 @@ +using System; + +namespace Microsoft.Windows.Controls.PropertyGrid.Attributes +{ + public class ItemsSourceAttribute : Attribute + { + public Type Type { get; set; } + + public ItemsSourceAttribute(Type type) + { + var valueSourceInterface = type.GetInterface("Microsoft.Windows.Controls.PropertyGrid.Attributes.IItemsSource"); + if (valueSourceInterface == null) + throw new ArgumentException("Type must implement the IItemsSource interface.", "type"); + + Type = type; + } + } +} \ No newline at end of file diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs new file mode 100644 index 00000000..c79bcd4d --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Windows.Controls.PropertyGrid.Attributes; + +namespace Microsoft.Windows.Controls.PropertyGrid.Editors +{ + public class ItemsSourceEditor : ComboBoxEditor + { + private ItemsSourceAttribute _attribute; + + public ItemsSourceEditor(ItemsSourceAttribute attribute) + { + _attribute = attribute; + } + + protected override IList CreateItemsSource(PropertyItem propertyItem) + { + var instance = Activator.CreateInstance(_attribute.Type); + return (instance as IItemsSource).GetValues(); + } + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs index 51b8cfed..c868c7f0 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs @@ -10,6 +10,7 @@ using System.ComponentModel; using System.Windows.Input; using Microsoft.Windows.Controls.PropertyGrid.Editors; using Microsoft.Windows.Controls.PropertyGrid.Commands; +using Microsoft.Windows.Controls.PropertyGrid.Attributes; namespace Microsoft.Windows.Controls.PropertyGrid { @@ -413,15 +414,42 @@ namespace Microsoft.Windows.Controls.PropertyGrid private FrameworkElement GetTypeEditor(PropertyItem propertyItem) { - FrameworkElement editor = GetCustomEditor(propertyItem, EditorDefinitions); + //first check for any attribute editor + FrameworkElement editor = GetAttibuteEditor(propertyItem); + //now look for a custom editor based on editor definitions + if (editor == null) + editor = GetCustomEditor(propertyItem, EditorDefinitions); + + //guess we have to use the default editor if (editor == null) editor = CreateDefaultEditor(propertyItem); return editor; } - private FrameworkElement GetCustomEditor(PropertyItem propertyItem, EditorDefinitionCollection customTypeEditors) + private static FrameworkElement GetAttibuteEditor(PropertyItem propertyItem) + { + FrameworkElement editor = null; + + var attribute = GetAttribute(propertyItem.PropertyDescriptor); + if (attribute != null) + editor = new ItemsSourceEditor(attribute).ResolveEditor(propertyItem); + + return editor; + } + + public static T GetAttribute(PropertyDescriptor property) where T : Attribute + { + foreach (Attribute att in property.Attributes) + { + var tAtt = att as T; + if (tAtt != null) return tAtt; + } + return null; + } + + private static FrameworkElement GetCustomEditor(PropertyItem propertyItem, EditorDefinitionCollection customTypeEditors) { FrameworkElement editor = null; @@ -446,7 +474,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid return editor; } - private FrameworkElement CreateDefaultEditor(PropertyItem propertyItem) + private static FrameworkElement CreateDefaultEditor(PropertyItem propertyItem) { ITypeEditor editor = null; diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj index 7191d9fe..282f6c85 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj @@ -237,6 +237,8 @@ True + + @@ -256,6 +258,7 @@ +