From f3a0d20ebd7baf8ef7afb030941261dc90bc3e7b Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Sat, 12 Nov 2011 03:33:46 +0000 Subject: [PATCH] PropertyGrid: added feature that allows you to specify a display name when using the IItemsSource attribute. --- .../Views/CustomItemsSource.xaml.cs | 32 +++++++++----- .../Implementation/Attributes/IItemsSource.cs | 28 +++++++++++- .../Editors/ItemsSourceAttributeEditor.cs | 43 +++++++++++++++++++ .../Editors/ItemsSourceEditor.cs | 24 ----------- .../Implementation/PropertyGridUtilities.cs | 2 +- .../WPFToolkit.Extended.csproj | 2 +- 6 files changed, 94 insertions(+), 37 deletions(-) create mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceAttributeEditor.cs delete mode 100644 ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs diff --git a/ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomItemsSource.xaml.cs b/ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomItemsSource.xaml.cs index 7afa8cb2..884c605f 100644 --- a/ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomItemsSource.xaml.cs +++ b/ExtendedWPFToolkitSolution/Src/Samples/Modules/Samples.Modules.PropertyGrid/Views/CustomItemsSource.xaml.cs @@ -1,11 +1,10 @@ -using Samples.Infrastructure.Controls; +using System; using System.ComponentModel; -using System.Collections.Generic; -using System; -using System.Windows.Media; using System.Windows; -using Microsoft.Windows.Controls.PropertyGrid.Attributes; +using System.Windows.Media; using Microsoft.Practices.Prism.Regions; +using Microsoft.Windows.Controls.PropertyGrid.Attributes; +using Samples.Infrastructure.Controls; namespace Samples.Modules.PropertyGrid.Views { @@ -70,12 +69,25 @@ namespace Samples.Modules.PropertyGrid.Views public class FontSizeItemsSource : IItemsSource { - public IList GetValues() + public ItemCollection GetValues() { - List sizes = new List() - { - 5.0,5.5,6.0,6.5,7.0,7.5,8.0,8.5,9.0,9.5,10.0,12.0,14.0,16.0,18.0,20.0 - }; + ItemCollection sizes = new ItemCollection(); + sizes.Add(5.0, "Five"); + sizes.Add(5.5); + sizes.Add(6.0, "Six"); + sizes.Add(6.5); + sizes.Add(7.0, "Seven"); + sizes.Add(7.5); + sizes.Add(8.0, "Eight"); + sizes.Add(8.5); + sizes.Add(9.0, "Nine"); + sizes.Add(9.5); + sizes.Add(10.0); + sizes.Add(12.0, "Twelve"); + sizes.Add(14.0); + sizes.Add(16.0); + sizes.Add(18.0); + sizes.Add(20.0); return sizes; } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs index bc8ac08b..ae017ae5 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs @@ -5,6 +5,32 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Attributes { public interface IItemsSource { - IList GetValues(); + ItemCollection GetValues(); + } + + public class Item + { + public string DisplayName { get; set; } + public object Value { get; set; } + } + + public class ItemCollection : List + { + public void Add(object value) + { + Item item = new Item(); + item.DisplayName = value.ToString(); + item.Value = value; + base.Add(item); + } + + public void Add(object value, string displayName) + { + Item newItem = new Item(); + newItem.DisplayName = displayName; + newItem.Value = value; + base.Add(newItem); + } + } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceAttributeEditor.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceAttributeEditor.cs new file mode 100644 index 00000000..f4a2e966 --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceAttributeEditor.cs @@ -0,0 +1,43 @@ +using System; +using Microsoft.Windows.Controls.PropertyGrid.Attributes; + +namespace Microsoft.Windows.Controls.PropertyGrid.Editors +{ + public class ItemsSourceAttributeEditor : TypeEditor + { + private readonly ItemsSourceAttribute _attribute; + + public ItemsSourceAttributeEditor(ItemsSourceAttribute attribute) + { + _attribute = attribute; + } + + protected override void SetValueDependencyProperty() + { + ValueProperty = System.Windows.Controls.ComboBox.SelectedValueProperty; + } + + protected override void ResolveValueBinding(PropertyItem propertyItem) + { + SetItemsSource(); + base.ResolveValueBinding(propertyItem); + } + + protected override void SetControlProperties() + { + Editor.DisplayMemberPath = "DisplayName"; + Editor.SelectedValuePath = "Value"; + } + + private void SetItemsSource() + { + Editor.ItemsSource = CreateItemsSource(); + } + + private System.Collections.IEnumerable CreateItemsSource() + { + var instance = Activator.CreateInstance(_attribute.Type); + return (instance as IItemsSource).GetValues(); + } + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs deleted file mode 100644 index c79bcd4d..00000000 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs +++ /dev/null @@ -1,24 +0,0 @@ -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/PropertyGridUtilities.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGridUtilities.cs index a0cf9e0a..de86f096 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGridUtilities.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGridUtilities.cs @@ -106,7 +106,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid var itemsSourceAttribute = GetAttribute(propertyItem.PropertyDescriptor); if (itemsSourceAttribute != null) - editor = new ItemsSourceEditor(itemsSourceAttribute).ResolveEditor(propertyItem); + editor = new ItemsSourceAttributeEditor(itemsSourceAttribute).ResolveEditor(propertyItem); var editorAttribute = GetAttribute(propertyItem.PropertyDescriptor); if (editorAttribute != null) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj index 5c7be4e7..f55a21d5 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj @@ -293,7 +293,7 @@ - +