Browse Source

PropertyGrid: implemented CustomTypeEditors and also did some refactoring.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
1f6dd01716
  1. 16
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditor.xaml.cs
  2. 4
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ComboBoxEditor.cs
  3. 3
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/ITypeEditor.cs
  4. 4
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/PrimitiveTypeCollectionEditor.cs
  5. 17
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/TypeEditor.cs
  6. 144
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs

16
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/CollectionEditor.xaml.cs

@ -17,16 +17,6 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors
InitializeComponent(); InitializeComponent();
} }
public void Attach(PropertyItem propertyItem)
{
_item = propertyItem;
}
public FrameworkElement ResolveEditor()
{
return this;
}
private void Button_Click(object sender, RoutedEventArgs e) private void Button_Click(object sender, RoutedEventArgs e)
{ {
CollectionEditorDialog editor = new CollectionEditorDialog(_item.PropertyType); CollectionEditorDialog editor = new CollectionEditorDialog(_item.PropertyType);
@ -36,5 +26,11 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors
BindingOperations.SetBinding(editor, CollectionEditorDialog.ItemsSourceProperty, binding); BindingOperations.SetBinding(editor, CollectionEditorDialog.ItemsSourceProperty, binding);
editor.ShowDialog(); editor.ShowDialog();
} }
public FrameworkElement ResolveEditor(PropertyItem propertyItem)
{
_item = propertyItem;
return this;
}
} }
} }

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

@ -10,10 +10,10 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors
ValueProperty = ComboBox.SelectedItemProperty; ValueProperty = ComboBox.SelectedItemProperty;
} }
public override void Attach(PropertyItem propertyItem) protected override void ResolveValueBinding(PropertyItem propertyItem)
{ {
SetItemsSource(propertyItem); SetItemsSource(propertyItem);
base.Attach(propertyItem); base.ResolveValueBinding(propertyItem);
} }
protected abstract IList<object> CreateItemsSource(PropertyItem propertyItem); protected abstract IList<object> CreateItemsSource(PropertyItem propertyItem);

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

@ -4,7 +4,6 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors
{ {
public interface ITypeEditor public interface ITypeEditor
{ {
void Attach(PropertyItem propertyItem); FrameworkElement ResolveEditor(PropertyItem propertyItem);
FrameworkElement ResolveEditor();
} }
} }

4
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Editors/PrimitiveTypeCollectionEditor.cs

@ -15,11 +15,11 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors
ValueProperty = Microsoft.Windows.Controls.PrimitiveTypeCollectionEditor.ItemsSourceProperty; ValueProperty = Microsoft.Windows.Controls.PrimitiveTypeCollectionEditor.ItemsSourceProperty;
} }
public override void Attach(PropertyItem propertyItem) protected override void ResolveValueBinding(PropertyItem propertyItem)
{ {
Editor.ItemsSourceType = propertyItem.PropertyType; Editor.ItemsSourceType = propertyItem.PropertyType;
Editor.ItemType = propertyItem.PropertyType.GetGenericArguments()[0]; Editor.ItemType = propertyItem.PropertyType.GetGenericArguments()[0];
base.Attach(propertyItem); base.ResolveValueBinding(propertyItem);
} }
} }
} }

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

@ -14,26 +14,14 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors
#endregion //Properties #endregion //Properties
#region Constructors #region ITypeEditor Members
public TypeEditor() public virtual FrameworkElement ResolveEditor(PropertyItem propertyItem)
{ {
Editor = new T(); Editor = new T();
SetValueDependencyProperty(); SetValueDependencyProperty();
SetControlProperties(); SetControlProperties();
}
#endregion //Constructors
#region ITypeEditor Members
public virtual void Attach(PropertyItem propertyItem)
{
ResolveValueBinding(propertyItem); ResolveValueBinding(propertyItem);
}
public virtual FrameworkElement ResolveEditor()
{
return Editor; return Editor;
} }
@ -64,7 +52,6 @@ namespace Microsoft.Windows.Controls.PropertyGrid.Editors
protected abstract void SetValueDependencyProperty(); protected abstract void SetValueDependencyProperty();
#endregion //Methods #endregion //Methods
} }
} }

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

@ -32,7 +32,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid
get { return (ContextMenu)GetValue(AdvancedOptionsMenuProperty); } get { return (ContextMenu)GetValue(AdvancedOptionsMenuProperty); }
set { SetValue(AdvancedOptionsMenuProperty, value); } set { SetValue(AdvancedOptionsMenuProperty, value); }
} }
#endregion //AdvancedOptionsMenu #endregion //AdvancedOptionsMenu
#region CustomTypeEditors #region CustomTypeEditors
@ -406,70 +406,8 @@ namespace Microsoft.Windows.Controls.PropertyGrid
}; };
propertyItem.SetBinding(PropertyItem.ValueProperty, binding); propertyItem.SetBinding(PropertyItem.ValueProperty, binding);
ITypeEditor editor = null; ITypeEditor editor = GetTypeEditor(propertyItem);
propertyItem.Editor = editor.ResolveEditor(propertyItem);
//check for custom editor
if (CustomTypeEditors.Count > 0)
{
//first check if the custom editor is type based
ICustomTypeEditor customEditor = CustomTypeEditors[propertyItem.PropertyType];
if (customEditor == null)
{
//must be property based
customEditor = CustomTypeEditors[propertyItem.Name];
}
if (customEditor != null)
editor = customEditor.Editor;
}
try
{
//no custom editor found
if (editor == null)
{
if (propertyItem.IsReadOnly)
editor = new TextBlockEditor();
else if (propertyItem.PropertyType == typeof(bool) || propertyItem.PropertyType == typeof(bool?))
editor = new CheckBoxEditor();
else if (propertyItem.PropertyType == typeof(decimal) || propertyItem.PropertyType == typeof(decimal?))
editor = new DecimalUpDownEditor();
else if (propertyItem.PropertyType == typeof(double) || propertyItem.PropertyType == typeof(double?))
editor = new DoubleUpDownEditor();
else if (propertyItem.PropertyType == typeof(int) || propertyItem.PropertyType == typeof(int?))
editor = new IntegerUpDownEditor();
else if (propertyItem.PropertyType == typeof(DateTime) || propertyItem.PropertyType == typeof(DateTime?))
editor = new DateTimeUpDownEditor();
else if ((propertyItem.PropertyType == typeof(Color)))
editor = new ColorEditor();
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 if (propertyItem.PropertyType.IsGenericType)
{
if (propertyItem.PropertyType.GetInterface("IList") != null)
{
var t = propertyItem.PropertyType.GetGenericArguments()[0];
if (!t.IsPrimitive && !t.Equals(typeof(String)))
editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.CollectionEditor();
else
editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.PrimitiveTypeCollectionEditor();
}
else
editor = new TextBlockEditor();
}
else
editor = new TextBoxEditor();
}
}
catch (Exception ex)
{
//TODO: handle this some how
}
editor.Attach(propertyItem);
propertyItem.Editor = editor.ResolveEditor();
return propertyItem; return propertyItem;
} }
@ -532,5 +470,81 @@ namespace Microsoft.Windows.Controls.PropertyGrid
} }
#endregion //Methods #endregion //Methods
private ITypeEditor GetTypeEditor(PropertyItem propertyItem)
{
ITypeEditor editor = null;
editor = CreateCustomEditor(propertyItem, CustomTypeEditors);
if (editor == null)
editor = CreateDefaultEditor(propertyItem);
return editor;
}
private ITypeEditor CreateCustomEditor(PropertyItem propertyItem, CustomTypeEditorCollection customTypeEditors)
{
ITypeEditor editor = null;
//check for custom editor
if (customTypeEditors.Count > 0)
{
//first check if the custom editor is type based
ICustomTypeEditor customEditor = customTypeEditors[propertyItem.PropertyType];
if (customEditor == null)
{
//must be property based
customEditor = customTypeEditors[propertyItem.Name];
}
if (customEditor != null)
editor = customEditor.Editor;
}
return editor;
}
private ITypeEditor CreateDefaultEditor(PropertyItem propertyItem)
{
ITypeEditor editor = null;
if (propertyItem.IsReadOnly)
editor = new TextBlockEditor();
else if (propertyItem.PropertyType == typeof(bool) || propertyItem.PropertyType == typeof(bool?))
editor = new CheckBoxEditor();
else if (propertyItem.PropertyType == typeof(decimal) || propertyItem.PropertyType == typeof(decimal?))
editor = new DecimalUpDownEditor();
else if (propertyItem.PropertyType == typeof(double) || propertyItem.PropertyType == typeof(double?))
editor = new DoubleUpDownEditor();
else if (propertyItem.PropertyType == typeof(int) || propertyItem.PropertyType == typeof(int?))
editor = new IntegerUpDownEditor();
else if (propertyItem.PropertyType == typeof(DateTime) || propertyItem.PropertyType == typeof(DateTime?))
editor = new DateTimeUpDownEditor();
else if ((propertyItem.PropertyType == typeof(Color)))
editor = new ColorEditor();
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 if (propertyItem.PropertyType.IsGenericType)
{
if (propertyItem.PropertyType.GetInterface("IList") != null)
{
var t = propertyItem.PropertyType.GetGenericArguments()[0];
if (!t.IsPrimitive && !t.Equals(typeof(String)))
editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.CollectionEditor();
else
editor = new Microsoft.Windows.Controls.PropertyGrid.Editors.PrimitiveTypeCollectionEditor();
}
else
editor = new TextBlockEditor();
}
else
editor = new TextBoxEditor();
return editor;
}
} }
} }

Loading…
Cancel
Save