Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ItemsControlCollectionViewBehavior.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Windows;
5 using System.Windows.Controls;
6 using System.Windows.Data;
7 using System.Windows.Interactivity;
8 
9 using SiliconStudio.Presentation.Core;
10 
11 namespace SiliconStudio.Presentation.Behaviors
12 {
13  public class ItemsControlCollectionViewBehavior : Behavior<ItemsControl>
14  {
15  private readonly DependencyPropertyWatcher propertyWatcher = new DependencyPropertyWatcher();
16 
17  public static readonly DependencyProperty GroupingPropertyNameProperty = DependencyProperty.Register("GroupingPropertyName", typeof(string), typeof(ItemsControlCollectionViewBehavior), new PropertyMetadata(null, GroupingPropertyNameChanged));
18 
19  public static readonly DependencyProperty FilterPredicateProperty = DependencyProperty.Register("FilterPredicate", typeof(Predicate<object>), typeof(ItemsControlCollectionViewBehavior), new PropertyMetadata(null, FilterPredicateChanged));
20 
21  public string GroupingPropertyName { get { return (string)GetValue(GroupingPropertyNameProperty); } set { SetValue(GroupingPropertyNameProperty, value); } }
22 
23  public Predicate<object> FilterPredicate { get { return (Predicate<object>)GetValue(FilterPredicateProperty); } set { SetValue(FilterPredicateProperty, value); } }
24 
25  public IValueConverter GroupingPropertyConverter { get; set; }
26 
27  protected override void OnAttached()
28  {
29  base.OnAttached();
30  propertyWatcher.Attach(AssociatedObject);
31  propertyWatcher.RegisterValueChangedHandler(ItemsControl.ItemsSourceProperty, ItemsSourceChanged);
32  UpdateCollectionView();
33  }
34 
35  protected override void OnDetaching()
36  {
37  propertyWatcher.Detach();
38  base.OnDetaching();
39  }
40 
41  private void UpdateCollectionView()
42  {
43  if (AssociatedObject != null && AssociatedObject.ItemsSource != null)
44  {
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())
48  {
49  bool removeGrouping = string.IsNullOrWhiteSpace(GroupingPropertyName);
50  if (collectionView.CanGroup)
51  {
52  if (collectionView.GroupDescriptions == null) throw new InvalidOperationException("CollectionView does not have a group description collection.");
53  collectionView.GroupDescriptions.Clear();
54 
55  if (!removeGrouping)
56  {
57  var groupDescription = new PropertyGroupDescription(GroupingPropertyName, GroupingPropertyConverter);
58  collectionView.GroupDescriptions.Add(groupDescription);
59  }
60  }
61  if (collectionView.CanFilter)
62  {
63  collectionView.Filter = FilterPredicate;
64  }
65  }
66  }
67  }
68 
69  private void ItemsSourceChanged(object sender, EventArgs e)
70  {
71  UpdateCollectionView();
72  }
73 
74  private static void GroupingPropertyNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
75  {
76  var behavior = (ItemsControlCollectionViewBehavior)d;
77  behavior.UpdateCollectionView();
78  }
79 
80  private static void FilterPredicateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
81  {
82  var behavior = (ItemsControlCollectionViewBehavior)d;
83  behavior.UpdateCollectionView();
84  }
85  }
86 }
87