Browse Source

PropertyGrid: Re-architected editors. This alllows you to create and provide custom editors.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
c1138ce229
  1. 23
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/CustomTypeEditorCollection.cs
  2. 45
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/CheckBoxEditorProvider.cs
  3. 67
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/EnumComboBoxEditorProvider.cs
  4. 110
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/FontComboBoxEditorProvider.cs
  5. 11
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/ITypeEditorProvider.cs
  6. 41
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/NumericUpDownEditorProvider.cs
  7. 42
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/TextBoxEditorProvider.cs
  8. 16
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CheckBoxEditor.cs
  9. 13
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ColorEditor.cs
  10. 27
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ComboBoxEditor.cs
  11. 16
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CustomTypeEditor.cs
  12. 41
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/EnumComboBoxEditor.cs
  13. 104
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/FontComboBoxEditor.cs
  14. 11
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ICustomTypeEditor.cs
  15. 10
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ITypeEditor.cs
  16. 14
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/TextBoxEditor.cs
  17. 61
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/TypeEditor.cs
  18. 68
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs
  19. 38
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs
  20. 17
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

23
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/CustomTypeEditorCollection.cs

@ -0,0 +1,23 @@
using System;
using System.Collections.ObjectModel;
using Microsoft.Windows.Controls.PropertyGrid.Editors;
namespace Microsoft.Windows.Controls.PropertyGrid
{
public class CustomTypeEditorCollection : ObservableCollection<ICustomTypeEditor>
{
public ICustomTypeEditor this[string propertyName]
{
get
{
foreach (var item in Items)
{
if (item.Properties.Contains(propertyName))
return item;
}
return null;
}
}
}
}

45
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/CheckBoxEditorProvider.cs

@ -1,45 +0,0 @@
using System;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
namespace Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders
{
public class CheckBoxEditorProvider : ITypeEditorProvider
{
CheckBox _checkbox;
public CheckBoxEditorProvider()
{
_checkbox = new CheckBox();
_checkbox.Margin = new Thickness(2, 0, 0, 0);
}
public void Initialize(PropertyItem propertyItem)
{
ResolveBinding(propertyItem);
}
public FrameworkElement ResolveEditor()
{
return _checkbox;
}
private void ResolveBinding(PropertyItem property)
{
var binding = new Binding(property.Name);
binding.Source = property.Instance;
binding.ValidatesOnExceptions = true;
binding.ValidatesOnDataErrors = true;
if (property.IsWriteable)
binding.Mode = BindingMode.TwoWay;
else
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(_checkbox, CheckBox.IsCheckedProperty, binding);
}
}
}

67
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/EnumComboBoxEditorProvider.cs

@ -1,67 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Reflection;
namespace Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders
{
public class EnumComboBoxEditorProvider : ITypeEditorProvider
{
ComboBox _comboBox;
public EnumComboBoxEditorProvider()
{
_comboBox = new ComboBox();
}
public void Initialize(PropertyItem propertyItem)
{
ResolveBinding(propertyItem);
SetItemsSource(propertyItem);
}
public FrameworkElement ResolveEditor()
{
return _comboBox;
}
private void ResolveBinding(PropertyItem property)
{
var binding = new Binding(property.Name);
binding.Source = property.Instance;
binding.ValidatesOnExceptions = true;
binding.ValidatesOnDataErrors = true;
if (property.IsWriteable)
binding.Mode = BindingMode.TwoWay;
else
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(_comboBox, ComboBox.SelectedItemProperty, binding);
}
private void SetItemsSource(PropertyItem property)
{
_comboBox.ItemsSource = GetValues(property.PropertyType);
}
public static object[] GetValues(Type enumType)
{
List<object> values = new List<object>();
var fields = from field in enumType.GetFields()
where field.IsLiteral
select field;
foreach (FieldInfo field in fields)
{
values.Add(field.GetValue(enumType));
}
return values.ToArray();
}
}
}

110
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/FontComboBoxEditorProvider.cs

@ -1,110 +0,0 @@
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
namespace Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders
{
public class FontComboBoxEditorProvider : ITypeEditorProvider
{
ComboBox _comboBox;
public FontComboBoxEditorProvider()
{
_comboBox = new ComboBox();
}
public void Initialize(PropertyItem propertyItem)
{
ResolveBinding(propertyItem);
SetItemsSource(propertyItem);
}
public FrameworkElement ResolveEditor()
{
return _comboBox;
}
private void ResolveBinding(PropertyItem property)
{
var binding = new Binding(property.Name);
binding.Source = property.Instance;
binding.ValidatesOnExceptions = true;
binding.ValidatesOnDataErrors = true;
if (property.IsWriteable)
binding.Mode = BindingMode.TwoWay;
else
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(_comboBox, ComboBox.SelectedItemProperty, binding);
}
private void SetItemsSource(PropertyItem property)
{
if (property.PropertyType == typeof(FontFamily))
{
List<FontFamily> fonts = new List<FontFamily>();
fonts.Add(new FontFamily("Arial"));
fonts.Add(new FontFamily("Courier New"));
fonts.Add(new FontFamily("Times New Roman"));
fonts.Add(new FontFamily("Batang"));
fonts.Add(new FontFamily("BatangChe"));
fonts.Add(new FontFamily("DFKai-SB"));
fonts.Add(new FontFamily("Dotum"));
fonts.Add(new FontFamily("DutumChe"));
fonts.Add(new FontFamily("FangSong"));
fonts.Add(new FontFamily("GulimChe"));
fonts.Add(new FontFamily("Gungsuh"));
fonts.Add(new FontFamily("GungsuhChe"));
fonts.Add(new FontFamily("KaiTi"));
fonts.Add(new FontFamily("Malgun Gothic"));
fonts.Add(new FontFamily("Meiryo"));
fonts.Add(new FontFamily("Microsoft JhengHei"));
fonts.Add(new FontFamily("Microsoft YaHei"));
fonts.Add(new FontFamily("MingLiU"));
fonts.Add(new FontFamily("MingLiu_HKSCS"));
fonts.Add(new FontFamily("MingLiu_HKSCS-ExtB"));
fonts.Add(new FontFamily("MingLiu-ExtB"));
_comboBox.ItemsSource = fonts;
}
else if (property.PropertyType == typeof(FontWeight))
{
List<FontWeight> list = new List<FontWeight>()
{
FontWeights.Black, FontWeights.Bold, FontWeights.ExtraBlack, FontWeights.ExtraBold,
FontWeights.ExtraLight, FontWeights.Light, FontWeights.Medium, FontWeights.Normal, FontWeights.SemiBold,
FontWeights.Thin
};
_comboBox.ItemsSource = list;
}
else if (property.PropertyType == typeof(FontStyle))
{
List<FontStyle> list = new List<FontStyle>()
{
FontStyles.Italic,
FontStyles.Normal
};
_comboBox.ItemsSource = list;
}
else if (property.PropertyType == typeof(FontStretch))
{
List<FontStretch> list = new List<FontStretch>()
{
FontStretches.Condensed,
FontStretches.Expanded,
FontStretches.ExtraCondensed,
FontStretches.ExtraExpanded,
FontStretches.Normal,
FontStretches.SemiCondensed,
FontStretches.SemiExpanded,
FontStretches.UltraCondensed,
FontStretches.UltraExpanded
};
_comboBox.ItemsSource = list;
}
}
}
}

11
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/ITypeEditorProvider.cs

@ -1,11 +0,0 @@
using System;
using System.Windows;
namespace Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders
{
interface ITypeEditorProvider
{
void Initialize(PropertyItem propertyItem);
FrameworkElement ResolveEditor();
}
}

41
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/NumericUpDownEditorProvider.cs

@ -1,41 +0,0 @@
using System;
using System.Windows.Data;
using System.Windows;
namespace Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders
{
public class NumericUpDownEditorProvider : ITypeEditorProvider
{
NumericUpDown _numericUpDown;
public NumericUpDownEditorProvider()
{
_numericUpDown = new NumericUpDown();
}
public void Initialize(PropertyItem propertyItem)
{
ResolveBinding(propertyItem);
}
public FrameworkElement ResolveEditor()
{
return _numericUpDown;
}
private void ResolveBinding(PropertyItem property)
{
var binding = new Binding(property.Name);
binding.Source = property.Instance;
binding.ValidatesOnExceptions = true;
binding.ValidatesOnDataErrors = true;
if (property.IsWriteable)
binding.Mode = BindingMode.TwoWay;
else
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(_numericUpDown, NumericUpDown.ValueProperty, binding);
}
}
}

42
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/EditorProviders/TextBoxEditorProvider.cs

@ -1,42 +0,0 @@
using System;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows;
namespace Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders
{
public class TextBoxEditorProvider : ITypeEditorProvider
{
FrameworkElement _editor;
public TextBoxEditorProvider()
{
_editor = new TextBox();
}
public void Initialize(PropertyItem propertyItem)
{
ResolveBinding(propertyItem);
}
public FrameworkElement ResolveEditor()
{
return _editor;
}
private void ResolveBinding(PropertyItem property)
{
var binding = new Binding(property.Name);
binding.Source = property.Instance;
binding.ValidatesOnExceptions = true;
binding.ValidatesOnDataErrors = true;
if (property.IsWriteable)
binding.Mode = BindingMode.TwoWay;
else
binding.Mode = BindingMode.OneWay;
BindingOperations.SetBinding(_editor, TextBox.TextProperty, binding);
}
}
}

16
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CheckBoxEditor.cs

@ -0,0 +1,16 @@
using System.Windows;
using System.Windows.Controls;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public class CheckBoxEditor : TypeEditor
{
protected override void Initialize()
{
Editor = new CheckBox();
Editor.Margin = new Thickness(4, 0, 0, 0);
ValueProperty = CheckBox.IsCheckedProperty;
}
}
}

13
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ColorEditor.cs

@ -0,0 +1,13 @@
using System;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public class ColorEditor : TypeEditor
{
protected override void Initialize()
{
Editor = new ColorPicker();
ValueProperty = ColorPicker.SelectedColorProperty;
}
}
}

27
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ComboBoxEditor.cs

@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Windows.Controls;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public abstract class ComboBoxEditor : TypeEditor
{
protected override void Initialize()
{
Editor = new ComboBox();
ValueProperty = ComboBox.SelectedItemProperty;
}
public override void Attach(PropertyItem propertyItem)
{
SetItemsSource(propertyItem);
base.Attach(propertyItem);
}
private void SetItemsSource(PropertyItem propertyItem)
{
(Editor as ComboBox).ItemsSource = CreateItemsSource(propertyItem);
}
protected abstract IList<object> CreateItemsSource(PropertyItem propertyItem);
}
}

16
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CustomTypeEditor.cs

@ -0,0 +1,16 @@
using System.Collections.Generic;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public class CustomTypeEditor : ICustomTypeEditor
{
public ITypeEditor Editor { get; set; }
private IList<string> _properties = new List<string>();
public IList<string> Properties
{
get { return _properties; }
set { _properties = value; }
}
}
}

41
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/EnumComboBoxEditor.cs

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Windows.Controls;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public class EnumComboBoxEditor : TypeEditor
{
protected override void Initialize()
{
Editor = new ComboBox();
ValueProperty = ComboBox.SelectedItemProperty;
}
public override void Attach(PropertyItem propertyItem)
{
SetItemsSource(propertyItem);
base.Attach(propertyItem);
}
private void SetItemsSource(PropertyItem propertyItem)
{
(Editor as ComboBox).ItemsSource = GetValues(propertyItem.PropertyType);
}
private static object[] GetValues(Type enumType)
{
List<object> values = new List<object>();
var fields = enumType.GetFields().Where(x => x.IsLiteral);
foreach (FieldInfo field in fields)
{
values.Add(field.GetValue(enumType));
}
return values.ToArray();
}
}
}

104
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/FontComboBoxEditor.cs

@ -0,0 +1,104 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public class FontComboBoxEditor : ComboBoxEditor
{
protected override IList<object> CreateItemsSource(PropertyItem propertyItem)
{
if (propertyItem.PropertyType == typeof(FontFamily))
{
return GetFontFamilies();
}
else if (propertyItem.PropertyType == typeof(FontWeight))
{
return GetFontWeights();
}
else if (propertyItem.PropertyType == typeof(FontStyle))
{
return GetFontStyles();
}
else if (propertyItem.PropertyType == typeof(FontStretch))
{
return GetFontStretches();
}
return null;
}
private static IList<object> GetFontFamilies()
{
IList<object> fontFamilies = new List<object>();
//TODO: get all fonts
fontFamilies.Add(new FontFamily("Arial"));
fontFamilies.Add(new FontFamily("Courier New"));
fontFamilies.Add(new FontFamily("Times New Roman"));
fontFamilies.Add(new FontFamily("Batang"));
fontFamilies.Add(new FontFamily("BatangChe"));
fontFamilies.Add(new FontFamily("DFKai-SB"));
fontFamilies.Add(new FontFamily("Dotum"));
fontFamilies.Add(new FontFamily("DutumChe"));
fontFamilies.Add(new FontFamily("FangSong"));
fontFamilies.Add(new FontFamily("GulimChe"));
fontFamilies.Add(new FontFamily("Gungsuh"));
fontFamilies.Add(new FontFamily("GungsuhChe"));
fontFamilies.Add(new FontFamily("KaiTi"));
fontFamilies.Add(new FontFamily("Malgun Gothic"));
fontFamilies.Add(new FontFamily("Meiryo"));
fontFamilies.Add(new FontFamily("Microsoft JhengHei"));
fontFamilies.Add(new FontFamily("Microsoft YaHei"));
fontFamilies.Add(new FontFamily("MingLiU"));
fontFamilies.Add(new FontFamily("MingLiu_HKSCS"));
fontFamilies.Add(new FontFamily("MingLiu_HKSCS-ExtB"));
fontFamilies.Add(new FontFamily("MingLiu-ExtB"));
fontFamilies.Add(new FontFamily("Segoe UI"));
return fontFamilies;
}
private static IList<object> GetFontWeights()
{
return new List<object>()
{
FontWeights.Black,
FontWeights.Bold,
FontWeights.ExtraBlack,
FontWeights.ExtraBold,
FontWeights.ExtraLight,
FontWeights.Light,
FontWeights.Medium,
FontWeights.Normal,
FontWeights.SemiBold,
FontWeights.Thin
};
}
private static IList<object> GetFontStyles()
{
return new List<object>()
{
FontStyles.Italic,
FontStyles.Normal
};
}
private static IList<object> GetFontStretches()
{
return new List<object>()
{
FontStretches.Condensed,
FontStretches.Expanded,
FontStretches.ExtraCondensed,
FontStretches.ExtraExpanded,
FontStretches.Normal,
FontStretches.SemiCondensed,
FontStretches.SemiExpanded,
FontStretches.UltraCondensed,
FontStretches.UltraExpanded
};
}
}
}

11
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ICustomTypeEditor.cs

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public interface ICustomTypeEditor
{
ITypeEditor Editor { get; set; }
IList<string> Properties { get; set; }
}
}

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

@ -0,0 +1,10 @@
using System.Windows;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public interface ITypeEditor
{
void Attach(PropertyItem propertyItem);
FrameworkElement ResolveEditor();
}
}

14
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/TextBoxEditor.cs

@ -0,0 +1,14 @@
using System;
using System.Windows.Controls;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public class TextBoxEditor : TypeEditor
{
protected override void Initialize()
{
Editor = new TextBox();
ValueProperty = TextBox.TextProperty;
}
}
}

61
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/TypeEditor.cs

@ -0,0 +1,61 @@
using System;
using System.Windows;
using System.Windows.Data;
namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{
public abstract class TypeEditor : ITypeEditor
{
#region Properties
protected FrameworkElement Editor { get; set; }
protected DependencyProperty ValueProperty { get; set; }
#endregion //Properties
#region Constructors
public TypeEditor()
{
Initialize();
}
#endregion //Constructors
#region ITypeEditor Members
public virtual void Attach(PropertyItem propertyItem)
{
ResolveBinding(propertyItem);
}
public virtual FrameworkElement ResolveEditor()
{
return Editor;
}
#endregion //ITypeEditor Members
#region Methods
protected abstract void Initialize();
protected virtual void ResolveBinding(PropertyItem propertyItem)
{
var _binding = new Binding("Value");
_binding.Source = propertyItem;
_binding.ValidatesOnExceptions = true;
_binding.ValidatesOnDataErrors = true;
_binding.Mode = propertyItem.IsWriteable ? BindingMode.TwoWay : BindingMode.OneWay;
_binding.Converter = CreateConverter();
BindingOperations.SetBinding(Editor, ValueProperty, _binding);
}
protected virtual IValueConverter CreateConverter()
{
return null;
}
#endregion //Methods
}
}

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

@ -7,8 +7,8 @@ using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls.Primitives;
using System.ComponentModel;
using Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders;
using System.Windows.Input;
using Microsoft.Windows.Controls.PropertyGrid.Editors;
namespace Microsoft.Windows.Controls.PropertyGrid
{
@ -23,6 +23,17 @@ namespace Microsoft.Windows.Controls.PropertyGrid
#region Properties
#region CustomTypeEditors
public static readonly DependencyProperty CustomTypeEditorsProperty = DependencyProperty.Register("CustomTypeEditors", typeof(CustomTypeEditorCollection), typeof(PropertyGrid), new UIPropertyMetadata(new CustomTypeEditorCollection()));
public CustomTypeEditorCollection CustomTypeEditors
{
get { return (CustomTypeEditorCollection)GetValue(CustomTypeEditorsProperty); }
set { SetValue(CustomTypeEditorsProperty, value); }
}
#endregion //CustomTypeEditors
#region IsCategorized
public static readonly DependencyProperty IsCategorizedProperty = DependencyProperty.Register("IsCategorized", typeof(bool), typeof(PropertyGrid), new UIPropertyMetadata(true, OnIsCategorizedChanged));
@ -237,30 +248,47 @@ namespace Microsoft.Windows.Controls.PropertyGrid
return propertyItems;
}
private static PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance, PropertyGrid grid)
private PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance, PropertyGrid grid)
{
PropertyItem propertyItem = new PropertyItem(instance, property, grid);
ITypeEditorProvider editorProvider = null;
if (propertyItem.PropertyType == typeof(string))
editorProvider = new TextBoxEditorProvider();
else if (propertyItem.PropertyType == typeof(bool))
editorProvider = new CheckBoxEditorProvider();
else if (propertyItem.PropertyType.IsEnum)
editorProvider = new EnumComboBoxEditorProvider();
else if (propertyItem.PropertyType == typeof(FontFamily) || propertyItem.PropertyType == typeof(FontWeight) || propertyItem.PropertyType == typeof(FontStyle) || propertyItem.PropertyType == typeof(FontStretch))
editorProvider = new FontComboBoxEditorProvider();
else if (propertyItem.PropertyType == typeof(double))
editorProvider = new TextBoxEditorProvider();
else if (propertyItem.PropertyType == typeof(object) || propertyItem.PropertyType == typeof(Thickness))
editorProvider = new TextBoxEditorProvider();
if (editorProvider != null)
var binding = new Binding(property.Name)
{
editorProvider.Initialize(propertyItem);
propertyItem.Editor = editorProvider.ResolveEditor();
Source = instance,
ValidatesOnExceptions = true,
ValidatesOnDataErrors = true,
Mode = propertyItem.IsWriteable ? BindingMode.TwoWay : BindingMode.OneWay
};
propertyItem.SetBinding(PropertyItem.ValueProperty, binding);
ITypeEditor editor = null;
//check for custom editor
if (CustomTypeEditors.Count > 0)
{
ICustomTypeEditor customEditor = CustomTypeEditors[propertyItem.Name];
if (customEditor != null)
{
editor = customEditor.Editor;
}
}
//no custom editor found
if (editor == null)
{
if (propertyItem.PropertyType == typeof(bool))
editor = new CheckBoxEditor();
else if (propertyItem.PropertyType.IsEnum)
editor = new EnumComboBoxEditor();
else if (propertyItem.PropertyType == typeof(FontFamily) || propertyItem.PropertyType == typeof(FontWeight) || propertyItem.PropertyType == typeof(FontStyle) || propertyItem.PropertyType == typeof(FontStretch))
editor = new FontComboBoxEditor();
else
editor = new TextBoxEditor();
}
editor.Attach(propertyItem);
propertyItem.Editor = editor.ResolveEditor();
return propertyItem;
}

38
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs

@ -69,6 +69,44 @@ namespace Microsoft.Windows.Controls.PropertyGrid
#endregion //Editor
#region Value
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(object), typeof(PropertyItem), new UIPropertyMetadata(null, new PropertyChangedCallback(OnValueChanged), new CoerceValueCallback(OnCoerceValue)));
public object Value
{
get { return (object)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
private static object OnCoerceValue(DependencyObject o, object value)
{
PropertyItem propertyItem = o as PropertyItem;
if (propertyItem != null)
return propertyItem.OnCoerceValue((object)value);
else
return value;
}
private static void OnValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
PropertyItem propertyItem = o as PropertyItem;
if (propertyItem != null)
propertyItem.OnValueChanged((object)e.OldValue, (object)e.NewValue);
}
protected virtual object OnCoerceValue(object value)
{
// TODO: Keep the proposed value within the desired range.
return value;
}
protected virtual void OnValueChanged(object oldValue, object newValue)
{
// TODO: Add your property changed side-effects. Descendants can override as well.
}
#endregion //Value
#endregion //Properties
#region Constructor

17
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -164,12 +164,17 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="BusyIndicator\Implementation\VisualStates.BusyIndicator.cs" />
<Compile Include="PropertyGrid\Implementation\EditorProviders\CheckBoxEditorProvider.cs" />
<Compile Include="PropertyGrid\Implementation\EditorProviders\EnumComboBoxEditorProvider.cs" />
<Compile Include="PropertyGrid\Implementation\EditorProviders\FontComboBoxEditorProvider.cs" />
<Compile Include="PropertyGrid\Implementation\EditorProviders\ITypeEditorProvider.cs" />
<Compile Include="PropertyGrid\Implementation\EditorProviders\NumericUpDownEditorProvider.cs" />
<Compile Include="PropertyGrid\Implementation\EditorProviders\TextBoxEditorProvider.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\CheckBoxEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\ColorEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\ComboBoxEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\CustomTypeEditor.cs" />
<Compile Include="PropertyGrid\Implementation\CustomTypeEditorCollection.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\EnumComboBoxEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\FontComboBoxEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\ICustomTypeEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\ITypeEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\TextBoxEditor.cs" />
<Compile Include="PropertyGrid\Implementation\Editors\TypeEditor.cs" />
<Compile Include="PropertyGrid\Implementation\PropertyGrid.cs" />
<Compile Include="PropertyGrid\Implementation\PropertyCategoryItem.cs" />
<Compile Include="PropertyGrid\Implementation\PropertyCollection.cs" />

Loading…
Cancel
Save