diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs index d0d2650f..9caa7645 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs @@ -8,6 +8,7 @@ using System.Windows.Media; using System.Windows.Controls.Primitives; using System.ComponentModel; using Microsoft.Windows.Controls.PropertyGrid.Implementation.EditorProviders; +using System.Windows.Input; namespace Microsoft.Windows.Controls.PropertyGrid { @@ -41,54 +42,30 @@ namespace Microsoft.Windows.Controls.PropertyGrid protected virtual void OnIsCategorizedChanged(bool oldValue, bool newValue) { LoadProperties(newValue); - } + } #endregion //IsCategorized #region NameWidth - public static readonly DependencyProperty NameWidthProperty = DependencyProperty.Register("NameWidth", typeof(double), typeof(PropertyGrid), new UIPropertyMetadata(120.0, OnNameWidthChanged)); - public double NameWidth + public static readonly DependencyProperty NameColumnWidthProperty = DependencyProperty.Register("NameColumnWidth", typeof(double), typeof(PropertyGrid), new UIPropertyMetadata(120.0)); + public double NameColumnWidth { - get { return (double)GetValue(NameWidthProperty); } - set { SetValue(NameWidthProperty, value); } - } - - private static void OnNameWidthChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - PropertyGrid propertyGrid = o as PropertyGrid; - if (propertyGrid != null) - propertyGrid.OnNameWidthChanged((double)e.OldValue, (double)e.NewValue); - } - - protected virtual void OnNameWidthChanged(double oldValue, double newValue) - { - // TODO: Add your property changed side-effects. Descendants can override as well. + get { return (double)GetValue(NameColumnWidthProperty); } + set { SetValue(NameColumnWidthProperty, value); } } #endregion //NameWidth #region Properties - public static readonly DependencyProperty PropertiesProperty = DependencyProperty.Register("Properties", typeof(PropertyCollection), typeof(PropertyGrid), new UIPropertyMetadata(null, OnPropertiesChanged)); + public static readonly DependencyProperty PropertiesProperty = DependencyProperty.Register("Properties", typeof(PropertyCollection), typeof(PropertyGrid), new UIPropertyMetadata(null)); public PropertyCollection Properties { get { return (PropertyCollection)GetValue(PropertiesProperty); } private set { SetValue(PropertiesProperty, value); } } - private static void OnPropertiesChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - PropertyGrid propertyGrid = o as PropertyGrid; - if (propertyGrid != null) - propertyGrid.OnPropertiesChanged((PropertyCollection)e.OldValue, (PropertyCollection)e.NewValue); - } - - protected virtual void OnPropertiesChanged(PropertyCollection oldValue, PropertyCollection newValue) - { - // TODO: Add your property changed side-effects. Descendants can override as well. - } - #endregion //Properties #region SelectedObject @@ -127,7 +104,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid public Type SelectedObjectType { get { return (Type)GetValue(SelectedObjectTypeProperty); } - set { SetValue(SelectedObjectTypeProperty, value); } + private set { SetValue(SelectedObjectTypeProperty, value); } } private static void OnSelectedObjectTypeChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) @@ -146,49 +123,52 @@ namespace Microsoft.Windows.Controls.PropertyGrid #region SelectedObjectTypeName - public static readonly DependencyProperty SelectedObjectTypeNameProperty = DependencyProperty.Register("SelectedObjectTypeName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty, OnSelectedObjectTypeNameChanged)); + public static readonly DependencyProperty SelectedObjectTypeNameProperty = DependencyProperty.Register("SelectedObjectTypeName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty)); public string SelectedObjectTypeName { get { return (string)GetValue(SelectedObjectTypeNameProperty); } - set { SetValue(SelectedObjectTypeNameProperty, value); } - } - - private static void OnSelectedObjectTypeNameChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - PropertyGrid propertyGrid = o as PropertyGrid; - if (propertyGrid != null) - propertyGrid.OnSelectedObjectTypeNameChanged((string)e.OldValue, (string)e.NewValue); - } - - protected virtual void OnSelectedObjectTypeNameChanged(string oldValue, string newValue) - { - // TODO: Add your property changed side-effects. Descendants can override as well. + private set { SetValue(SelectedObjectTypeNameProperty, value); } } #endregion //SelectedObjectTypeName #region SelectedObjectName - public static readonly DependencyProperty SelectedObjectNameProperty = DependencyProperty.Register("SelectedObjectName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty, OnSelectedObjectNameChanged)); + public static readonly DependencyProperty SelectedObjectNameProperty = DependencyProperty.Register("SelectedObjectName", typeof(string), typeof(PropertyGrid), new UIPropertyMetadata(string.Empty)); public string SelectedObjectName { get { return (string)GetValue(SelectedObjectNameProperty); } - set { SetValue(SelectedObjectNameProperty, value); } + private set { SetValue(SelectedObjectNameProperty, value); } + } + + #endregion //SelectedObjectName + + #region SelectedProperty + + public static readonly DependencyProperty SelectedPropertyProperty = DependencyProperty.Register("SelectedProperty", typeof(PropertyItem), typeof(PropertyGrid), new UIPropertyMetadata(null, OnSelectedPropertyChanged)); + public PropertyItem SelectedProperty + { + get { return (PropertyItem)GetValue(SelectedPropertyProperty); } + internal set { SetValue(SelectedPropertyProperty, value); } } - private static void OnSelectedObjectNameChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) + private static void OnSelectedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { PropertyGrid propertyGrid = o as PropertyGrid; if (propertyGrid != null) - propertyGrid.OnSelectedObjectNameChanged((string)e.OldValue, (string)e.NewValue); + propertyGrid.OnSelectedPropertyChanged((PropertyItem)e.OldValue, (PropertyItem)e.NewValue); } - protected virtual void OnSelectedObjectNameChanged(string oldValue, string newValue) + protected virtual void OnSelectedPropertyChanged(PropertyItem oldValue, PropertyItem newValue) { - // TODO: Add your property changed side-effects. Descendants can override as well. - } + if (oldValue != null) + oldValue.IsSelected = false; - #endregion //SelectedObjectName + if (newValue != null) + newValue.IsSelected = true; + } + + #endregion //SelectedProperty #endregion //Properties @@ -211,13 +191,23 @@ namespace Microsoft.Windows.Controls.PropertyGrid _dragThumb.DragDelta += DragThumb_DragDelta; } + protected override void OnPreviewKeyDown(KeyEventArgs e) + { + //hitting enter on textbox will update value of underlying source + if (this.SelectedProperty != null && e.Key == Key.Enter && e.OriginalSource is TextBox) + { + BindingExpression be = ((TextBox)e.OriginalSource).GetBindingExpression(TextBox.TextProperty); + be.UpdateSource(); + } + } + #endregion //Base Class Overrides #region Event Handlers void DragThumb_DragDelta(object sender, DragDeltaEventArgs e) { - NameWidth = Math.Max(0, NameWidth + e.HorizontalChange); + NameColumnWidth = Math.Max(0, NameColumnWidth + e.HorizontalChange); } #endregion //Event Handlers @@ -232,7 +222,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid Properties = GetAlphabetizedProperties(_propertyItemsCache); } - private static List GetObjectProperties(object instance) + private List GetObjectProperties(object instance) { var propertyItems = new List(); if (instance == null) @@ -243,14 +233,14 @@ namespace Microsoft.Windows.Controls.PropertyGrid // Get all properties of the type propertyItems.AddRange(properties.Cast(). Where(p => p.IsBrowsable && p.Name != "GenericParameterAttributes"). - Select(property => CreatePropertyItem(property, instance))); + Select(property => CreatePropertyItem(property, instance, this))); return propertyItems; } - private static PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance) + private static PropertyItem CreatePropertyItem(PropertyDescriptor property, object instance, PropertyGrid grid) { - PropertyItem propertyItem = new PropertyItem(instance, property); + PropertyItem propertyItem = new PropertyItem(instance, property, grid); ITypeEditorProvider editorProvider = null; if (propertyItem.PropertyType == typeof(string)) @@ -275,20 +265,18 @@ namespace Microsoft.Windows.Controls.PropertyGrid return propertyItem; } - private PropertyCollection GetCategorizedProperties(List propertyItems) + private static PropertyCollection GetCategorizedProperties(List propertyItems) { PropertyCollection propertyCollection = new PropertyCollection(); - CollectionViewSource src = new CollectionViewSource(); - src.Source = propertyItems; + CollectionViewSource src = new CollectionViewSource { Source = propertyItems }; src.GroupDescriptions.Add(new PropertyGroupDescription("Category")); src.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending)); src.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); foreach (CollectionViewGroup item in src.View.Groups) { - PropertyCategoryItem propertyCategoryItem = new PropertyCategoryItem(); - propertyCategoryItem.Category = item.Name.ToString(); + PropertyCategoryItem propertyCategoryItem = new PropertyCategoryItem { Category = item.Name.ToString() }; foreach (var propertyitem in item.Items) { propertyCategoryItem.Properties.Add((PropertyItem)propertyitem); @@ -299,15 +287,14 @@ namespace Microsoft.Windows.Controls.PropertyGrid return propertyCollection; } - private PropertyCollection GetAlphabetizedProperties(List propertyItems) + private static PropertyCollection GetAlphabetizedProperties(List propertyItems) { PropertyCollection propertyCollection = new PropertyCollection(); if (propertyItems == null) return propertyCollection; - CollectionViewSource src = new CollectionViewSource(); - src.Source = propertyItems; + CollectionViewSource src = new CollectionViewSource { Source = propertyItems }; src.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); foreach (var item in ((ListCollectionView)(src.View))) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs index 74c5eff3..7e8b9213 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyItem.cs @@ -7,104 +7,66 @@ namespace Microsoft.Windows.Controls.PropertyGrid { public class PropertyItem : Control { - #region Members - - public object Instance { get; private set; } - public PropertyDescriptor PropertyDescriptor { get; private set; } - - #endregion //Members - #region Properties - public string Description - { - get { return PropertyDescriptor.Description; } - } + #region Category - public bool IsWriteable + public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Category", typeof(string), typeof(PropertyItem), new UIPropertyMetadata(string.Empty)); + public string Category { - get { return !IsReadOnly; } + get { return (string)GetValue(CategoryProperty); } + set { SetValue(CategoryProperty, value); } } - public bool IsReadOnly - { - get { return PropertyDescriptor.IsReadOnly; } - } + #endregion //Category - public Type PropertyType - { - get { return PropertyDescriptor.PropertyType; } - } + public string Description { get { return PropertyDescriptor.Description; } } - //public string Category - //{ - // get { return PropertyDescriptor.Category; } - //} + public object Instance { get; private set; } + + public bool IsReadOnly { get { return PropertyDescriptor.IsReadOnly; } } - public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Category", typeof(string), typeof(PropertyItem), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(OnCategoryChanged), new CoerceValueCallback(OnCoerceCategory))); + #region IsSelected - private static object OnCoerceCategory(DependencyObject o, object value) + public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(PropertyItem), new UIPropertyMetadata(false, OnIsSelectedChanged)); + public bool IsSelected { - PropertyItem propertyItem = o as PropertyItem; - if (propertyItem != null) - return propertyItem.OnCoerceCategory((string)value); - else - return value; + get { return (bool)GetValue(IsSelectedProperty); } + set { SetValue(IsSelectedProperty, value); } } - private static void OnCategoryChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) + private static void OnIsSelectedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { PropertyItem propertyItem = o as PropertyItem; if (propertyItem != null) - propertyItem.OnCategoryChanged((string)e.OldValue, (string)e.NewValue); + propertyItem.OnIsSelectedChanged((bool)e.OldValue, (bool)e.NewValue); } - protected virtual string OnCoerceCategory(string value) + protected virtual void OnIsSelectedChanged(bool oldValue, bool newValue) { - // TODO: Keep the proposed value within the desired range. - return value; + if (newValue) + PropertyGrid.SelectedProperty = this; } - protected virtual void OnCategoryChanged(string oldValue, string newValue) - { - // TODO: Add your property changed side-effects. Descendants can override as well. - } + #endregion //IsSelected - public string Category - { - // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property! - get - { - return (string)GetValue(CategoryProperty); - } - set - { - SetValue(CategoryProperty, value); - } - } + public bool IsWriteable { get { return !IsReadOnly; } } + + public PropertyDescriptor PropertyDescriptor { get; private set; } + + public PropertyGrid PropertyGrid { get; private set; } + public Type PropertyType { get { return PropertyDescriptor.PropertyType; } } #region Editor - public static readonly DependencyProperty EditorProperty = DependencyProperty.Register("Editor", typeof(FrameworkElement), typeof(PropertyItem), new UIPropertyMetadata(null, OnEditorChanged)); + public static readonly DependencyProperty EditorProperty = DependencyProperty.Register("Editor", typeof(FrameworkElement), typeof(PropertyItem), new UIPropertyMetadata(null)); public FrameworkElement Editor { get { return (FrameworkElement)GetValue(EditorProperty); } set { SetValue(EditorProperty, value); } } - private static void OnEditorChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - PropertyItem propertyItem = o as PropertyItem; - if (propertyItem != null) - propertyItem.OnEditorChanged((FrameworkElement)e.OldValue, (FrameworkElement)e.NewValue); - } - - protected virtual void OnEditorChanged(FrameworkElement oldValue, FrameworkElement newValue) - { - // TODO: Add your property changed side-effects. Descendants can override as well. - } - #endregion //Editor #endregion //Properties @@ -116,25 +78,24 @@ namespace Microsoft.Windows.Controls.PropertyGrid DefaultStyleKeyProperty.OverrideMetadata(typeof(PropertyItem), new FrameworkPropertyMetadata(typeof(PropertyItem))); } - public PropertyItem(object instance, PropertyDescriptor property) + public PropertyItem(object instance, PropertyDescriptor property, PropertyGrid propertyGrid) { Instance = instance; PropertyDescriptor = property; Name = PropertyDescriptor.Name; Category = PropertyDescriptor.Category; + PropertyGrid = propertyGrid; } #endregion //Constructor - #region INotifyPropertyChanged Members + #region Base Class Overrides - public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; - protected void OnPropertyChanged(string propertyName) + protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e) { - if (PropertyChanged != null) - PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); + IsSelected = true; } - #endregion + #endregion //Base Class Overrides } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Themes/Generic.xaml index bd546678..04c2541b 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Themes/Generic.xaml @@ -102,13 +102,15 @@