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
-
Subscribe to the
Loadedevent 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; } } } -
Set the
Commandproperty of the RadButton element to null and subscribe to itsClickevent. In the added event handler, clear the Text property of the cachedTextBox, as well as theSearchTextproperty of itsDataContext. Its DataContext will be of the typeSearchViewModel.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 = ""; } -
Subscribe to the
LostKeyboardFocusevent of the search-as-you-typeTextBox. 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:
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 = "";
}
}