I am building a WPF MVVM application.
I have a DataGrid, like so:
<DataGrid
Name=“Employees”
AutoGenerateColumns=“False”
CanUserAddRows=“False”
EnableColumnVirtualization=“True”
EnableRowVirtualization=“True”
ItemsSource={Binding EmployeesCollectionView}
SelectionUnit=“FullRow”
VirtualizingPanel.VirtualizationMode=“Recycling” //... />
and a TextBox inside one of the column's header:
<TextBox
x:Name="tbSearchName"
Text="{Binding DataContext.SearchName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType=UserControl}}"
TextWrapping="Wrap" />
ViewModel.cs:
public ObservableCollection<Employee> Employees
{
get => employees;
set
{
//set with NotifyPropertyChanged
EmployeesCollectionView = CollectionViewSource.GetDefaultView(employees);
}
}
public ICollectionView EmployeesCollectionView
{
get => employeesCollectionView;
set => //set with NotifyPropertyChanged
}
public string SearchName
{
get => searchName;
set => //set with NotifyPropertyChanged
}
I want to implement a Ctrl+F and F3 search (similar like the browser one). This means:
Ctrl+F - tbSearchName becomes active and the user starts typing
On every key press SearchName is updated
The first occurrence of SearchName is highlighted and when clicking on F3 it navigates to the next appearance and highlights it, and so on
If there are no occurrences, the tbSearchName's border turns red
What is the best way to implement that? Does everything happen in the code-behind or is there a MVVM way to do that?
