Browse Source

PropertyGrid: added filtering

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
3dbf1e4d9f
  1. BIN
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Images/ClearFilter16.png
  2. 17
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/Commands/PropertyGridCommands.cs
  3. 72
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCategoryItem.cs
  4. 35
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCollection.cs
  5. 61
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyGrid.cs

BIN
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Images/ClearFilter16.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 794 B

17
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;
}
}
}
}

72
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/PropertyGrid/Implementation/PropertyCategoryItem.cs

@ -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<PropertyItem> _Properties = new List<PropertyItem>();
public List<PropertyItem> Properties
{
get
{
return _Properties;
}
set
{
_Properties = value;
}
}
static PropertyCategoryItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(PropertyCategoryItem), new FrameworkPropertyMetadata(typeof(PropertyCategoryItem)));
}
}
}

35
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<object>
public class PropertyCollection : ObservableCollection<PropertyItem>
{
public PropertyCollection()
{
}
public PropertyCollection(List<object> list)
public PropertyCollection(List<PropertyItem> list)
: base(list)
{
}
public PropertyCollection(IEnumerable<object> collection)
public PropertyCollection(IEnumerable<PropertyItem> 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());
};
}
}
}

61
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<PropertyItem> _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<PropertyItem> 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<PropertyItem> 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;
}

Loading…
Cancel
Save