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 @@ +