diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Images/ClearFilter16.png b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Images/ClearFilter16.png new file mode 100644 index 00000000..aeaeab8d Binary files /dev/null and b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Images/ClearFilter16.png differ diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Commands/PropertyGridCommands.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Commands/PropertyGridCommands.cs new file mode 100644 index 00000000..4291ed48 --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Commands/PropertyGridCommands.cs @@ -0,0 +1,17 @@ +using System; +using System.Windows.Input; + +namespace Microsoft.Windows.Controls.PropertyGrid.Commands +{ + public class PropertyGridCommands + { + private static RoutedCommand _clearFilterCommand = new RoutedCommand(); + public static RoutedCommand ClearFilter + { + get + { + return _clearFilterCommand; + } + } + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCategoryItem.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCategoryItem.cs deleted file mode 100644 index 27089598..00000000 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCategoryItem.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Windows.Controls; -using System.Windows; - -namespace Microsoft.Windows.Controls.PropertyGrid -{ - public class PropertyCategoryItem : Control - { - public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register("Category", typeof(string), typeof(PropertyCategoryItem), new UIPropertyMetadata(String.Empty, new PropertyChangedCallback(OnCategoryChanged), new CoerceValueCallback(OnCoerceCategory))); - - private static object OnCoerceCategory(DependencyObject o, object value) - { - PropertyCategoryItem propertyCategoryItem = o as PropertyCategoryItem; - if (propertyCategoryItem != null) - return propertyCategoryItem.OnCoerceCategory((string)value); - else - return value; - } - - private static void OnCategoryChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) - { - PropertyCategoryItem propertyCategoryItem = o as PropertyCategoryItem; - if (propertyCategoryItem != null) - propertyCategoryItem.OnCategoryChanged((string)e.OldValue, (string)e.NewValue); - } - - protected virtual string OnCoerceCategory(string value) - { - // TODO: Keep the proposed value within the desired range. - return value; - } - - protected virtual void OnCategoryChanged(string oldValue, string newValue) - { - // TODO: Add your property changed side-effects. Descendants can override as well. - } - - 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); - } - } - - - private List _Properties = new List(); - public List Properties - { - get - { - return _Properties; - } - set - { - _Properties = value; - } - } - - - static PropertyCategoryItem() - { - DefaultStyleKeyProperty.OverrideMetadata(typeof(PropertyCategoryItem), new FrameworkPropertyMetadata(typeof(PropertyCategoryItem))); - } - } -} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCollection.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCollection.cs index 95834089..e2853929 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCollection.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCollection.cs @@ -1,24 +1,51 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Windows.Data; namespace Microsoft.Windows.Controls.PropertyGrid { - public class PropertyCollection : ObservableCollection + public class PropertyCollection : ObservableCollection { public PropertyCollection() { } - public PropertyCollection(List list) + + public PropertyCollection(List list) : base(list) { } - public PropertyCollection(IEnumerable collection) + public PropertyCollection(IEnumerable collection) : base(collection) { - + + } + + private ICollectionView GetDefaultView() + { + return CollectionViewSource.GetDefaultView(this); + } + + public void GroupBy(string name) + { + GetDefaultView().GroupDescriptions.Add(new PropertyGroupDescription(name)); + } + + public void SortBy(string name, ListSortDirection sortDirection) + { + GetDefaultView().SortDescriptions.Add(new SortDescription(name, sortDirection)); + } + + public void Filter(string text) + { + GetDefaultView().Filter = (item) => + { + var property = item as PropertyItem; + return property.Name.ToLower().StartsWith(text.ToLower()); + }; } } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs index c13c0a03..4e4611f1 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs @@ -9,6 +9,7 @@ using System.Windows.Controls.Primitives; using System.ComponentModel; using System.Windows.Input; using Microsoft.Windows.Controls.PropertyGrid.Editors; +using Microsoft.Windows.Controls.PropertyGrid.Commands; namespace Microsoft.Windows.Controls.PropertyGrid { @@ -18,7 +19,6 @@ namespace Microsoft.Windows.Controls.PropertyGrid private Thumb _dragThumb; private List _propertyItemsCache; - private CollectionViewSource _collectionViewSource; #endregion //Members @@ -64,7 +64,8 @@ namespace Microsoft.Windows.Controls.PropertyGrid protected virtual void OnFilterChanged(string oldValue, string newValue) { - + if (Properties != null) + Properties.Filter(newValue); } #endregion //Filter @@ -246,8 +247,7 @@ namespace Microsoft.Windows.Controls.PropertyGrid public PropertyGrid() { - CollectionViewSource collectionView = new CollectionViewSource() { Source = Properties }; - Resources.Add("PropertiesSource", collectionView); + CommandBindings.Add(new CommandBinding(PropertyGridCommands.ClearFilter, ClearFilter, CanClearFilter)); } #endregion //Constructors @@ -283,6 +283,20 @@ namespace Microsoft.Windows.Controls.PropertyGrid #endregion //Event Handlers + #region Commands + + private void ClearFilter(object sender, ExecutedRoutedEventArgs e) + { + Filter = String.Empty; + } + + private void CanClearFilter(object sender, CanExecuteRoutedEventArgs e) + { + e.CanExecute = !String.IsNullOrEmpty(Filter); + } + + #endregion //Commands + #region Methods private void InitializePropertyGrid(bool isCategorized) @@ -293,6 +307,9 @@ namespace Microsoft.Windows.Controls.PropertyGrid private void LoadProperties(bool isCategorized) { + //clear any filters first + Filter = String.Empty; + if (isCategorized) Properties = GetCategorizedProperties(_propertyItemsCache); else @@ -410,41 +427,17 @@ namespace Microsoft.Windows.Controls.PropertyGrid private static PropertyCollection GetCategorizedProperties(List propertyItems) { - PropertyCollection propertyCollection = new PropertyCollection(); - - 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 { Category = item.Name.ToString() }; - foreach (var propertyitem in item.Items) - { - propertyCategoryItem.Properties.Add((PropertyItem)propertyitem); - } - propertyCollection.Add(propertyCategoryItem); - } - + PropertyCollection propertyCollection = new PropertyCollection(propertyItems); + propertyCollection.GroupBy("Category"); + propertyCollection.SortBy("Category", ListSortDirection.Ascending); + propertyCollection.SortBy("Name", ListSortDirection.Ascending); return propertyCollection; } private static PropertyCollection GetAlphabetizedProperties(List propertyItems) { - PropertyCollection propertyCollection = new PropertyCollection(); - - if (propertyItems == null) - return propertyCollection; - - CollectionViewSource src = new CollectionViewSource { Source = propertyItems }; - src.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending)); - - foreach (var item in ((ListCollectionView)(src.View))) - { - propertyCollection.Add((PropertyItem)item); - } - + PropertyCollection propertyCollection = new PropertyCollection(propertyItems); + propertyCollection.SortBy("Name", ListSortDirection.Ascending); return propertyCollection; }