Browse Source

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<object> GetValues()
        {
            return new List<object>() { 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().
pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
b3152155b1
  1. 10
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/IItemsSource.cs
  2. 18
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/ItemsSourceAttribute.cs
  3. 24
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ItemsSourceEditor.cs
  4. 34
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs
  5. 3
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

10
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<object> GetValues();
}
}

18
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;
}
}
}

24
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<object> CreateItemsSource(PropertyItem propertyItem)
{
var instance = Activator.CreateInstance(_attribute.Type);
return (instance as IItemsSource).GetValues();
}
}
}

34
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<ItemsSourceAttribute>(propertyItem.PropertyDescriptor);
if (attribute != null)
editor = new ItemsSourceEditor(attribute).ResolveEditor(propertyItem);
return editor;
}
public static T GetAttribute<T>(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;

3
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -237,6 +237,8 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="BusyIndicator\Implementation\VisualStates.BusyIndicator.cs" />
<Compile Include="PropertyGrid\Implementation\Attributes\IItemsSource.cs" />
<Compile Include="PropertyGrid\Implementation\Attributes\ItemsSourceAttribute.cs" />
<Compile Include="PropertyGrid\Implementation\Commands\PropertyGridCommands.cs" />
<Compile Include="PropertyGrid\Implementation\Converters\ValueSourceToImagePathConverter.cs" />
<Compile Include="PropertyGrid\Implementation\Converters\ValueSourceToToolTipConverter.cs" />
@ -256,6 +258,7 @@
<Compile Include="PropertyGrid\Implementation\Editors\DoubleUpDownEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\EnumComboBoxEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\FontComboBoxEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\ItemsSourceEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\TimeSpanEditor.cs" />
<Compile Include="PropertyGrid\Implementation\IEditorDefinition.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\IntegerUpDownEditor.cs" />

Loading…
Cancel
Save