5 using System.Windows.Controls;
6 using System.Windows.Data;
7 using System.Windows.Interactivity;
9 using SiliconStudio.Presentation.Core;
11 namespace SiliconStudio.Presentation.Behaviors
17 public static readonly DependencyProperty GroupingPropertyNameProperty = DependencyProperty.Register(
"GroupingPropertyName", typeof(
string), typeof(
ItemsControlCollectionViewBehavior),
new PropertyMetadata(null, GroupingPropertyNameChanged));
19 public static readonly DependencyProperty FilterPredicateProperty = DependencyProperty.Register(
"FilterPredicate", typeof(Predicate<object>), typeof(
ItemsControlCollectionViewBehavior),
new PropertyMetadata(null, FilterPredicateChanged));
21 public string GroupingPropertyName {
get {
return (
string)GetValue(GroupingPropertyNameProperty); } set { SetValue(GroupingPropertyNameProperty, value); } }
23 public Predicate<object> FilterPredicate {
get {
return (Predicate<object>)GetValue(FilterPredicateProperty); } set { SetValue(FilterPredicateProperty, value); } }
30 propertyWatcher.Attach(AssociatedObject);
31 propertyWatcher.RegisterValueChangedHandler(ItemsControl.ItemsSourceProperty, ItemsSourceChanged);
32 UpdateCollectionView();
37 propertyWatcher.Detach();
41 private void UpdateCollectionView()
43 if (AssociatedObject != null && AssociatedObject.ItemsSource != null)
45 var collectionView = (CollectionView)CollectionViewSource.GetDefaultView(AssociatedObject.ItemsSource);
46 if (collectionView == null)
throw new InvalidOperationException(
"CollectionViewSource.GetDefaultView returned null for the items source of the associated object.");
47 using (collectionView.DeferRefresh())
49 bool removeGrouping = string.IsNullOrWhiteSpace(GroupingPropertyName);
50 if (collectionView.CanGroup)
52 if (collectionView.GroupDescriptions == null)
throw new InvalidOperationException(
"CollectionView does not have a group description collection.");
53 collectionView.GroupDescriptions.Clear();
57 var groupDescription =
new PropertyGroupDescription(GroupingPropertyName, GroupingPropertyConverter);
58 collectionView.GroupDescriptions.Add(groupDescription);
61 if (collectionView.CanFilter)
63 collectionView.Filter = FilterPredicate;
69 private void ItemsSourceChanged(
object sender,
EventArgs e)
71 UpdateCollectionView();
74 private static void GroupingPropertyNameChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
76 var behavior = (ItemsControlCollectionViewBehavior)d;
77 behavior.UpdateCollectionView();
80 private static void FilterPredicateChanged(
DependencyObject d, DependencyPropertyChangedEventArgs e)
82 var behavior = (ItemsControlCollectionViewBehavior)d;
83 behavior.UpdateCollectionView();
override void OnAttached()
override void OnDetaching()