This is a migrated thread and some comments may be shown as answers.

Close Filter Popup on Enter Key

6 Answers 39 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Todd
Top achievements
Rank 1
Todd asked on 15 Aug 2014, 04:13 PM
I have a request from end users to have the GridView Filter Popup window closed when the Enter key is hit.  Is this possible?

6 Answers, 1 is accepted

Sort by
0
Boris
Telerik team
answered on 19 Aug 2014, 10:39 AM
Hello Todd,

In order to close filtering Popup on pressing the Enter key, you will need to create a custom class that derives from FilteringControl. Then you can use the ChildrenOfType<T>() extension method to find the filtering TextBox and then subscribe to its KeyDown event. In addition to close the Popup, you need to set its IsOpen property to false. To do that you will need to find it first. A possible way to achieve this is to use the ParentOfType<T>() extension method.

Also, if you want to close the popup when the Filter button is pressed you need to override the OnApplyFilter method of the FilteringControl. For example you can do something like the following: 

public class MyFilteringControl : FilteringControl
    {
        public MyFilteringControl(Telerik.Windows.Controls.GridViewColumn column)
            : base(column)
        {
            this.DefaultStyleKey = typeof(MyFilteringControl);
            this.Loaded += new System.Windows.RoutedEventHandler(MyFilteringControl_Loaded);
        }
 
        void MyFilteringControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                var tb = this.ChildrenOfType<TextBox>().FirstOrDefault();
                if (tb != null)
                {
                    tb.Focus();
                    tb.KeyDown += new System.Windows.Input.KeyEventHandler(tb_KeyDown);
                }
            }));
 
        }
 
        void tb_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.ParentOfType<Popup>().IsOpen = false;
            }
        }
 
        protected override void OnApplyFilter()
        {
            base.OnApplyFilter();
 
            var popup = this.ParentOfType<System.Windows.Controls.Primitives.Popup>();
            if (popup != null)
            {
                popup.IsOpen = false;
            }
        }
    }

Then you can apply it to a desired column like so:

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            this.clubsGrid.Columns["Name"].FilteringControl = new MyFilteringControl(this.clubsGrid.Columns["Name"]);
        }
    }

I attached a sample project that demonstrates the suggested approach. The example uses NoXAML binaries. However, if you need the example to work with the standard binaries including theme dlls, you will need to remove the ResourceDictionary in the App.xaml file.

For more information about the different types of binaries you can check the Setting a Theme (Using Implicit Styles) article.

Please let us know if this helps.

Regards,
Boris Penev
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Zohreh
Top achievements
Rank 1
answered on 08 Jun 2016, 04:57 AM

Hi Boris,

If you do it across the whole application, It can be done with behavior much easier.

just need to add a behavior to FilteringDropDown in your GridViewHeaderCellTemplate.

0
Stefan
Telerik team
answered on 10 Jun 2016, 11:34 AM
Hi Zohreh,

Indeed, your suggestion seems reasonable. Thank you for sharing it with the community.

All the best,
Stefan X1
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Todd
Top achievements
Rank 1
answered on 10 Jun 2016, 11:46 AM
Could you provide an example?  It would be very helpful.  
0
Zohreh
Top achievements
Rank 1
answered on 07 Jul 2016, 12:57 AM

This is an example of the behavior I have created:

  public class EnterKeyClosesPopupBehavior : Behavior<FilteringDropDown>
    {
        protected override void OnAttached()
        {
            AssociatedObject.KeyDown += AssociatedObject_KeyDown;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.KeyDown -= AssociatedObject_KeyDown;
        }

        private void AssociatedObject_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                AssociatedObject.IsDropDownOpen = false;
            }
        }
    }

 

and then in GridViewHeaderCellTemplate you need to attach this behavior to the filtering popup, something like this:

  <grid:FilteringDropDown x:Name="PART_DistinctFilterControl" Grid.Column="1" Visibility="{TemplateBinding FilteringUIVisibility}"                                             Margin="0,0,-2,0">
                         <i:Interaction.Behaviors>
                     <EnterKeyClosesPopupBehavior />
                    </i:Interaction.Behaviors>
  </grid:FilteringDropDown>

 

hope you find this helpful.

0
Todd
Top achievements
Rank 1
answered on 02 Aug 2016, 06:22 PM

Hi Zohreh,

I finally got the time to look at this.  I have the code in place and see that the OnAttached and OnDetaching methods are being called, but the KeyDown is never called.  Is this a problem with the keypress events not bubbling up to the filter popup?

Todd

Tags
GridView
Asked by
Todd
Top achievements
Rank 1
Answers by
Boris
Telerik team
Zohreh
Top achievements
Rank 1
Stefan
Telerik team
Todd
Top achievements
Rank 1
Share this question
or