New to Telerik UI for WPFStart a free 30-day trial

GridView Clear Input Before Commit in Deferred Search Mode

Updated on Sep 24, 2025

Environment

Product Version2022.3.912
ProductRadGridView for WPF

Description

When the IsSearchingDeferred property of RadGridView is set to True, the clear button will remove the search input only if it is committed.

This article will show you how to clear the search value every time the clear button is clicked.

Solution

  1. Subscribe to the Loaded event of RadGridView and retrieve the search-as-you-type TextBox element and cache it. Then, retrieve the RadButton control used for clearing the input. This can be done using the ChildrenOfType extension method.

    Retrieve the search-as-you-type TextBox and clear RadButton elements

    C#
    	public partial class MainWindow : Window
    	{
    	    private TextBox searchTextBox;
    
    	    public MainWindow()
    	    {
    	        InitializeComponent();
    
    			this.radGridView.Loaded += OnRadGridViewLoaded;
    	    }
    
    	    private void OnRadGridViewLoaded(object sender, RoutedEventArgs e)
    	    {
    	        RadGridView radGridView = (RadGridView)sender;
    
    	        TextBox searchAsYouTypeTextBox = radGridView.ChildrenOfType<TextBox>().FirstOrDefault(x => x.Name == "PART_SearchAsYouTypeTextBox");
    	        RadButton clearButton = radGridView.ChildrenOfType<RadButton>().FirstOrDefault(x => x.Name == "ClearButton");
    
    	        if (searchAsYouTypeTextBox != null && clearButton != null)
    	        {
    	            this.searchTextBox = searchAsYouTypeTextBox;
    	        }
    	    }
    	}
  2. Set the Command property of the RadButton element to null and subscribe to its Click event. In the added event handler, clear the Text property of the cached TextBox, as well as the SearchText property of its DataContext. Its DataContext will be of the type SearchViewModel.

    Subscribe to the Click event of the clear RadButton

    C#
    	clearButton.Command = null;
        clearButton.Click += ClearButton_Click;
    
    	private void ClearButton_Click(object sender, RoutedEventArgs e)
    	{
        	this.searchTextBox.Text = "";
        	((SearchViewModel)this.searchTextBox.DataContext).SearchText = "";
    	}
  3. Subscribe to the LostKeyboardFocus event of the search-as-you-type TextBox. In the added event handler, if clear RadButton is the new element that receives the focus, raise its Click event.

    Subscribe to the LostKeyboardFocus event of the search-as-you-type TextBox

    C#
    	this.searchTextBox.LostKeyboardFocus += SearchTextBox_LostKeyboardFocus;
    
    	private void SearchTextBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    	{
    	    if (e.NewFocus is RadButton radbutton)
    	    {
    	        if (radbutton.Name == "ClearButton")
    	        {
    	            radbutton.RaiseEvent(new RoutedEventArgs(RadButton.ClickEvent));
    	        }
    	    }
    	}

The full implementation of this solution is as follows:

C#
	public partial class MainWindow : Window
	{
	    private TextBox searchTextBox;
	
	    public MainWindow()
	    {
	        InitializeComponent();
	    }

	    private void RadGridView_Loaded(object sender, RoutedEventArgs e)
	    {
	        RadGridView radGridView = (RadGridView)sender;

	        TextBox searchAsYouTypeTextBox = radGridView.ChildrenOfType<TextBox>().FirstOrDefault(x => x.Name == "PART_SearchAsYouTypeTextBox");
	        RadButton clearButton = radGridView.ChildrenOfType<RadButton>().FirstOrDefault(x => x.Name == "ClearButton");

	        if (searchAsYouTypeTextBox != null && clearButton != null)
	        {
	            this.searchTextBox = searchAsYouTypeTextBox;
	            this.searchTextBox.LostKeyboardFocus += SearchTextBox_LostKeyboardFocus;

	            clearButton.Command = null;
	            clearButton.Click += ClearButton_Click;
	        }
	    }

	    private void SearchTextBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
	    {
	        if (e.NewFocus is RadButton radbutton)
	        {
	            if (radbutton.Name == "ClearButton")
	            {
	                radbutton.RaiseEvent(new RoutedEventArgs(RadButton.ClickEvent));
	            }
	        }
	    }

	    private void ClearButton_Click(object sender, RoutedEventArgs e)
	    {
	        this.searchTextBox.Text = "";
	        ((SearchViewModel)this.searchTextBox.DataContext).SearchText = "";
	    }
	}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support