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

Strange "behavior" using a behavior

1 Answer 63 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Håkan
Top achievements
Rank 1
Håkan asked on 29 Jan 2014, 01:27 PM
Hi!

When using the filter control I would like the apply filter button to close the filter control.
I found a behavior that takes care of this looking like this:

using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
 
namespace Soe.Silverlight.Behaviors
{
    /// <summary>
    /// A behavior that closes the filtering popup of a column when the Apply Filter button is clicked.
    /// </summary>
    public class ClosePopupOnApplyFilterBehavior : Behavior<GridViewBoundColumnBase>
    {
        FilteringControl customFilteringControl;
        Button applyFilterButton;
 
        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
        protected override void OnAttached()
        {
            // This is the control that RadGridView uses internally if you don't specify a custom filtering control through the GridViewBoundColumnBase.FilteringControl property.
            // We will create an instance of this default filtering control and use it as a "custom" filtering control in order to extend just a little bit with our custom logic.
            // Everything else will be the same.
            this.customFilteringControl = new FilteringControl();
            this.customFilteringControl.Loaded += this.OnFilteringControlLoaded;
 
            // Tell the column to use our new "custom" filtering control.
            // It will never now that this is the default one but spicied up a little.
            this.AssociatedObject.FilteringControl = customFilteringControl;
        }
 
        private void OnFilteringControlLoaded(object sender, RoutedEventArgs e)
        {
            // When it loads and all of its children are alive find the "Filter" button which is the only button on the control.
            // You can find out what its name is from the FilteringControl template.
            this.applyFilterButton = this.customFilteringControl.ChildrenOfType<Button>().Where(b => b.Name == "PART_ApplyFilterButton").FirstOrDefault();
            if (this.applyFilterButton != null)
                this.applyFilterButton.Click += this.OnApplyFilter;
        }
 
        private void OnApplyFilter(object sender, RoutedEventArgs e)
        {
            // And when clicked find the parent popup and close it.
            var popup = applyFilterButton.ParentOfType<System.Windows.Controls.Primitives.Popup>();
            if (popup != null)
                popup.IsOpen = false;
        }
 
        /// <summary>
        /// Called when the behavior is being detached from its AssociatedObject,
        /// but before it has actually occurred.
        /// </summary>
        /// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
        protected override void OnDetaching()
        {
            if (this.applyFilterButton != null)
                this.applyFilterButton.Click -= this.OnApplyFilter;
 
            this.customFilteringControl.Loaded -= this.OnFilteringControlLoaded;
        }
    }
}

The problem is that when I use it, the funnel is displayed before any data is loaded into the grid.
As default, the funnel is only visible when the grid contains data.
If I click on the funnel when the grid is empty I get the following exception:

This columns cannot be filtered.
 
 
   at Telerik.Windows.Controls.GridViewColumn.get_AvailableFilterOperators()
   at Telerik.Windows.Controls.GridViewColumn.Telerik.Windows.Controls.IFilterableColumn.get_AvailableFilterOperators()
   at Telerik.Windows.Controls.GridViewColumn.Telerik.Windows.Controls.IFilterableColumn.CallFilterOperatorsLoading()
   at Telerik.Windows.Controls.GridView.FilteringViewModel.RefreshFieldFilters()
   at Telerik.Windows.Controls.GridView.FilteringViewModel.Refresh()
   at Telerik.Windows.Controls.GridView.FilteringControl.Prepare(GridViewColumn column)
   at Telerik.Windows.Controls.GridView.FilteringDropDown.PrepareFilteringControl()
   at Telerik.Windows.Controls.GridView.FilteringDropDown.OnDropDownPopupOpened(Object sender, EventArgs e)
   at Telerik.Windows.Controls.AutoClosePopupWrapper.OnPopupOpened(Object sender, EventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(UInt32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)

How can I work around this?

Is there any better way than using a behavior for it?

Regads,
Håkan

1 Answer, 1 is accepted

Sort by
0
Accepted
Yoan
Telerik team
answered on 03 Feb 2014, 12:11 PM
Hi Håkan,

In order to workaround this behaviour you can disable RadGridView's filtering functionality when its is empty. You can set the IsFilteringAllowed property to false and set it back to true when DataLoaded event is fired. In this way the funnel will not appear till the RadGridView is filled with items.

I hope this helps.

Regards,
Yoan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for SILVERLIGHT.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Håkan
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Share this question
or