Browse Source

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; }
        }
pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
96a65ec051
  1. 18
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Attributes/TypeEditorAttribute.cs
  2. 15
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs
  3. 1
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

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

15
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<ItemsSourceAttribute>(propertyItem.PropertyDescriptor);
if (attribute != null)
editor = new ItemsSourceEditor(attribute).ResolveEditor(propertyItem);
var itemsSourceAttribute = GetAttribute<ItemsSourceAttribute>(propertyItem.PropertyDescriptor);
if (itemsSourceAttribute != null)
editor = new ItemsSourceEditor(itemsSourceAttribute).ResolveEditor(propertyItem);
var editorAttribute = GetAttribute<TypeEditorAttribute>(propertyItem.PropertyDescriptor);
if (editorAttribute != null)
{
var instance = Activator.CreateInstance(editorAttribute.Type);
editor = (instance as ITypeEditor).ResolveEditor(propertyItem);
}
return editor;
}

1
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -237,6 +237,7 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="BusyIndicator\Implementation\VisualStates.BusyIndicator.cs" />
<Compile Include="PropertyGrid\Implementation\Attributes\TypeEditorAttribute.cs" />
<Compile Include="PropertyGrid\Implementation\Attributes\IItemsSource.cs" />
<Compile Include="PropertyGrid\Implementation\Attributes\ItemsSourceAttribute.cs" />
<Compile Include="PropertyGrid\Implementation\Commands\PropertyGridCommands.cs" />

Loading…
Cancel
Save