Browse Source

PropertyGrid: Deleted custom TypeEditorAttribute for defining editors and instead use the already common EditorAttribute provided my the .NET framework. Editors MUST implement the ITypeEditor interface. So now to declare custom editors you define them as follows:

private TimeSpan _timeSpan;
        [Editor(typeof(MyCustomEditor), typeof(MyCustomEditor))]
        public TimeSpan TimeSpan
        {
            get { return _timeSpan; }
            set { _timeSpan = value; }
        }

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

@ -1,18 +0,0 @@
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;
}
}
}

10
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs

@ -407,7 +407,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid
}; };
propertyItem.SetBinding(PropertyItem.ValueProperty, binding); propertyItem.SetBinding(PropertyItem.ValueProperty, binding);
propertyItem.Editor = GetTypeEditor(propertyItem); propertyItem.Editor = GetTypeEditor(propertyItem);
return propertyItem; return propertyItem;
} }
@ -436,11 +436,13 @@ namespace Microsoft.Windows.Controls.PropertyGrid
if (itemsSourceAttribute != null) if (itemsSourceAttribute != null)
editor = new ItemsSourceEditor(itemsSourceAttribute).ResolveEditor(propertyItem); editor = new ItemsSourceEditor(itemsSourceAttribute).ResolveEditor(propertyItem);
var editorAttribute = GetAttribute<TypeEditorAttribute>(propertyItem.PropertyDescriptor); var editorAttribute = GetAttribute<EditorAttribute>(propertyItem.PropertyDescriptor);
if (editorAttribute != null) if (editorAttribute != null)
{ {
var instance = Activator.CreateInstance(editorAttribute.Type); Type type = Type.GetType(editorAttribute.EditorTypeName);
editor = (instance as ITypeEditor).ResolveEditor(propertyItem); var instance = Activator.CreateInstance(type);
if (instance is ITypeEditor)
editor = (instance as ITypeEditor).ResolveEditor(propertyItem);
} }
return editor; return editor;

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

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

Loading…
Cancel
Save