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

Clearing an active custom filter when last item is unchecked : UWP Grid

2 Answers 116 Views
DataGrid
This is a migrated thread and some comments may be shown as answers.
JFK
Top achievements
Rank 1
JFK asked on 26 Jun 2019, 06:33 PM

I've implemented custom filtering based on the how-to article in the KB, but I see this odd behavior where, after clearing the last checkbox in my filter, the filter control appears to still run the PassesFilter logic, resulting in 0 matches, and the column header still has the visual treatment indicating that it is filtered. What I'd like to do is have an un-check of the last item in a filter behave as if the user has clicked the 'Clear Filter' button.

I've tried several things and havethe following conclusions:

  • The custom filter is applied to the column when the CommandId.FilterButtonTap command executes, but the Clear button executes CommandId.FilterRequested, so if I want to reset the filter I'd need to do so in my custom GridFilterRequestedCommand
  • IsFiltering and Descriptor are both read-only properties on the FilterRequestContext in CommandId.FilterRequested so I can't try to set the first to false or null the second
  • It looks like the Clear Filters button removes the FilterDescriptor from the owner, but this didn't make any difference when I tried it because I can't un-set IsFiltering
  • I can't create my own FilterRequestedContext to fake an invocation because of the internal sets on all the properties
  • WPF appears to have a ClearFilters method which UWP lacks

I'm hoping there's some way to hack around these shortcomings. I also think that a CommandId.FilterRequested should be able to notify the Grid column filter that there are no items chosen to filter on, and that the column should act as if the filter has been cleared.

2 Answers, 1 is accepted

Sort by
0
JFK
Top achievements
Rank 1
answered on 27 Jun 2019, 11:57 PM
context.GetType()?.GetProperty("IsFiltering")?.SetValue(context, false);

 

0
Nasko
Telerik team
answered on 28 Jun 2019, 08:41 AM
Hello Joshua,

I have noticed you have opened an issue in the public repository. I am copying my answer here as well, so other people from the community could benefit from it:

The described by you behavior is not caused by the DataGrid control, but by the custom logic implemented in the article.

It is expected when the Apply button is pressed the command to execute filtering logic not the one for clearing. You can modify that behavior of the custom filtering ui by creating a custom FilterRequested command as you have already found out. Inside the Execute logic you can check if there are any checked items and only then to execute the default implementation of the command:

public class CustomApplyFilterCommand : DataGridCommand
{
    public CustomApplyFilterCommand()
    {
        this.Id = CommandId.FilterRequested;
    }
 
    public override bool CanExecute(object parameter)
    {
        return this.Owner.CommandService.CanExecuteDefaultCommand(CommandId.FilterRequested, parameter);
    }
 
    public override void Execute(object parameter)
    {
        var dataGrid = this.Owner;
        var context = (FilterRequestedContext)parameter;
        var descriptor = context.Descriptor as DelegateFilterDescriptor;
        if (descriptor != null)
        {
            var filter = descriptor.Filter as ColorFilter;
            if (filter != null && filter.Colors.Any(a => a.IsChecked))
            {
                dataGrid.CommandService.ExecuteDefaultCommand(CommandId.FilterRequested, parameter);
            }
        }
    }
}

The filter descriptor will be cleared when the apply button is clicked and if the default command is not executed no filter will be applied and thus the desired behavior will be achieved.

I hope the provided solution will work for.

If you have any additional questions or concerns please, let me know.

Regards,
Nasko
Progress Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
DataGrid
Asked by
JFK
Top achievements
Rank 1
Answers by
JFK
Top achievements
Rank 1
Nasko
Telerik team
Share this question
or