1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using Telerik.Windows.Controls;
6: using System.Windows;
7: using Telerik.Windows.Controls.GridView;
8: using System.Windows.Controls;
9: using System.Windows.Interactivity;
10:
11: namespace ClosePopupOnApplyFilter
12: {
13: /// <summary>
14: /// A behavior that closes the filtering popup of a column when the Apply Filter
15: /// button is clicked.
16: /// </summary>
17: public class ClosePopupOnApplyFilterBehavior : Behavior<GridViewBoundColumnBase>
18: {
19: FilteringControl customFilteringControl;
20: Button applyFilterButton;
21:
22: /// <summary>
23: /// Called after the behavior is attached to an AssociatedObject.
24: /// </summary>
25: /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
26: protected override void OnAttached()
27: {
28: // This is the control that RadGridView uses internally if you
29: // don't specify a custom filtering control through the
30: // GridViewBoundColumnBase.FilteringControl property.
31: // We will create an instance of this default filtering control
32: // and use it as a "custom" filtering control in order to extend
33: // just a little bit with our custom logic. Everything else will
34: // be the same.
35: this.customFilteringControl = new FilteringControl();
36: this.customFilteringControl.Loaded += this.OnFilteringControlLoaded;
37:
38: // Tell the column to use our new "custom" filtering control.
39: // It will never now that this is the default one but spicied up a little.
40: this.AssociatedObject.FilteringControl = customFilteringControl;
41: }
42:
43: void OnFilteringControlLoaded(object sender, RoutedEventArgs e)
44: {
45: // When it loads and all of its children are alive find the "Filter"
46: // button which is the only button on the control.
47: // You can find out what its name is from the FilteringControl template.
48: this.applyFilterButton = this.customFilteringControl
49: .ChildrenOfType<Button>()
50: .Where(b => b.Name == "PART_ApplyFilterButton")
51: .FirstOrDefault();
52:
53: if (this.applyFilterButton != null)
54: {
55: this.applyFilterButton.Click += this.OnApplyFilter;
56: }
57: }
58:
59: void OnApplyFilter(object sender, RoutedEventArgs e)
60: {
61: // And when clicked find the parent popup and close it.
62: var popup = applyFilterButton.ParentOfType<System.Windows.Controls.Primitives.Popup>();
63: if (popup != null)
64: {
65: popup.IsOpen = false;
66: }
67: }
68:
69: /// <summary>
70: /// Called when the behavior is being detached from its AssociatedObject,
71: /// but before it has actually occurred.
72: /// </summary>
73: /// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
74: protected override void OnDetaching()
75: {
76: if (this.applyFilterButton != null)
77: {
78: this.applyFilterButton.Click -= this.OnApplyFilter;
79: }
80:
81: this.customFilteringControl.Loaded -= this.OnFilteringControlLoaded;
82: }
83: }
84: }