If you are familiar with the RadGridView‘s Custom Filtering Functionality you probably know that you can create any kind of user control to replace the default one which looks like this:
Very often, however, you may be perfectly happy with the stock filtering control, but you wish you could modify and adapt it just a little bit to match your particular requirements. What should you do then? Maybe build an entirely new filtering control from scratch that looks just like RadGridView’s default one and adds this tiny bit of functionality? No, that would be insane. There is an easier way to do this. Read on.
A couple of our customers have recently asked the same question. How can I close the filtering popup when the user clicks the Filter Button?
By design, the filtering popup closes when the user clicks outside it or on the little funnel in the header cell. We have decided to implement it this way because clicking the Filter Button does not necessarily mean that the user has finished filtering. He or she might want to continue after seeing what the results are. So we decided not to close the the popup until the user explicitly decides that he or she is done filtering by clicking somewhere else with the mouse.
But this can be changed in a matter of seconds and I will try to explain how.
I have developed a simple attached behavior that makes the filtering popup close when the button is clicked. You attach it to a GridViewBoundColumnBase to let it know it should behave in this new way. To make a behavior work you simply need to override its OnAttached and OnDetaching methods. Let’s take a look at my behavior:
What it does is the following. It creates a new instance of RadGridView’s default FilteringControl. Then it attaches to its Loaded event. The Loaded event will be fired when the user clicks the filtering funnel. When this happens, I locate the Filter Button and attach to its Click event. When the user clicks the button, I find the popup that this control is in and close. That’s it. On detaching I simply clear the event handlers. To use the Behavior class you will need a reference to the System.Windows.Interactivity assembly.
To learn about behaviors in greater detail you can follow Shawn Wildremuth’s great blog post.
You can turn on this behavior entirely in XAML. You do not have to write in the code-behind file. Here is how to do it:
In my sample I have attached the behavior to the Name column only. Of course you can do this in the code-behind too if you want to. You can also do it for a specific column, for all columns, when a new column is auto-generated, etc. Use it anyway you want to.
You can even extend this behavior to do something else, for example close the popup when the Clear Filter Button is clicked. That is the beauty of attached behaviors – they are general purpose and you got to love them.
Here are the links to the full source code of my sample project:
Let me know if you come up with other cool ways to use attached behaviors with RadGridView.
Subscribe to be the first to get our expert-written articles and tutorials for developers!