New to Telerik UI for WPF? Download free 30-day trial

GridView Clear Input Before Commit in Deferred Search Mode

Environment

Product Version 2022.3.912
Product RadGridView 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

        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; 
                } 
            } 
        } 
    
        Public Partial Class MainWindow 
            Inherits Window 
            Private searchTextBox As TextBox 
     
            Public Sub New() 
                InitializeComponent() 
     
                Me.radGridView.Loaded += AddressOf OnRadGridViewLoaded 
            End Sub 
     
            Private Sub OnRadGridViewLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs) 
                Dim radGridView As RadGridView = CType(sender, RadGridView) 
     
                Dim searchAsYouTypeTextBox As TextBox = radGridView.ChildrenOfType(Of TextBox)().FirstOrDefault(Function(x) x.Name Is "PART_SearchAsYouTypeTextBox") 
                Dim clearButton As RadButton = radGridView.ChildrenOfType(Of RadButton)().FirstOrDefault(Function(x) x.Name Is "ClearButton") 
     
                If searchAsYouTypeTextBox IsNot Nothing AndAlso clearButton IsNot Nothing Then 
                    searchTextBox = searchAsYouTypeTextBox 
                End If 
            End Sub 
        End Class 
    
  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

        clearButton.Command = null; 
        clearButton.Click += ClearButton_Click; 
     
        private void ClearButton_Click(object sender, RoutedEventArgs e) 
        { 
            this.searchTextBox.Text = ""; 
            ((SearchViewModel)this.searchTextBox.DataContext).SearchText = ""; 
        } 
    
        clearButton.Command = Nothing 
        clearButton.Click += AddressOf ClearButton_Click 
     
        Private Sub ClearButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
            searchTextBox.Text = "" 
            CType(searchTextBox.DataContext, SearchViewModel).SearchText = "" 
        End Sub 
    
  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

        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)); 
                } 
            } 
        } 
    
        Me.searchTextBox.LostKeyboardFocus += AddressOf SearchTextBox_LostKeyboardFocus 
     
        Private Sub SearchTextBox_LostKeyboardFocus(ByVal sender As Object, ByVal e As KeyboardFocusChangedEventArgs) 
        Dim radbutton As RadButton = Nothing 
     
            If CSharpImpl.__Assign(radbutton, TryCast(e.NewFocus, RadButton)) IsNot Nothing Then 
                If radbutton.Name Is "ClearButton" Then 
                    radbutton.RaiseEvent 
                End If 
            End If 
        End Sub 
     
        Private Class CSharpImpl 
            Shared Function __Assign(Of T)(ByRef target As T, value As T) As T 
                target = value 
                Return value 
            End Function 
        End Class 
    

The full implementation of this solution is as follows:

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 = ""; 
    } 
} 
Public Partial Class MainWindow 
    Inherits Window 
 
    Private searchTextBox As TextBox 
 
    Public Sub New() 
        InitializeComponent() 
    End Sub 
 
    Private Sub RadGridView_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) 
        Dim radGridView As RadGridView = CType(sender, RadGridView) 
        Dim searchAsYouTypeTextBox As TextBox = radGridView.ChildrenOfType(Of TextBox)().FirstOrDefault(Function(x) x.Name = "PART_SearchAsYouTypeTextBox") 
        Dim clearButton As RadButton = radGridView.ChildrenOfType(Of RadButton)().FirstOrDefault(Function(x) x.Name = "ClearButton") 
 
        If searchAsYouTypeTextBox IsNot Nothing AndAlso clearButton IsNot Nothing Then 
            Me.searchTextBox = searchAsYouTypeTextBox 
            Me.searchTextBox.LostKeyboardFocus += AddressOf SearchTextBox_LostKeyboardFocus 
            clearButton.Command = Nothing 
            clearButton.Click += AddressOf ClearButton_Click 
        End If 
    End Sub 
 
    Private Sub SearchTextBox_LostKeyboardFocus(ByVal sender As Object, ByVal e As KeyboardFocusChangedEventArgs) 
        Dim radbutton As RadButton = Nothing 
 
        If CSharpImpl.__Assign(radbutton, TryCast(e.NewFocus, RadButton)) IsNot Nothing Then 
 
            If radbutton.Name = "ClearButton" Then 
                radbutton.RaiseEvent 
            End If 
        End If 
    End Sub 
 
    Private Sub ClearButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) 
        Me.searchTextBox.Text = "" 
        (CType(Me.searchTextBox.DataContext, SearchViewModel)).SearchText = "" 
    End Sub 
 
    Private Class CSharpImpl 
        Shared Function __Assign(Of T)(ByRef target As T, value As T) As T 
            target = value 
            Return value 
        End Function 
    End Class 
End Class 
In this article