I use the following code for testing. If it meets your needs, you could refer to it.
MainWindow.xaml:
<StackPanel>
<TextBox Text="{Binding TextSearch,UpdateSourceTrigger=PropertyChanged}"/>
<DataGrid Name="datagrid" VerticalAlignment="Top" SelectedItem="{Binding SelectedEmployee}"
ItemsSource="{Binding View}" AutoGenerateColumns="False" ScrollViewer.VerticalScrollBarVisibility="Visible" >
<DataGrid.Columns>
<DataGridTextColumn Header="FirstName" Binding="{Binding FirstName}" Width="80"/>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new EmployeeListViewModel();
}
}
class EmployeeListViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public EmployeeListViewModel()
{
EmployeeList = new ObservableCollection<Employee>(GetEmployees());
this._view = new ListCollectionView(this.employeeList);
this._view.Filter=Filter;
}
private bool Filter(object item)
{
if(String.IsNullOrEmpty(TextSearch))
return true;
else
return((item as Employee).FirstName.IndexOf(TextSearch,StringComparison.OrdinalIgnoreCase) >=0);
}
private ObservableCollection<Employee> employeeList;
public ObservableCollection<Employee> EmployeeList
{
get { return employeeList; }
set
{
employeeList = value;
OnPropertyChanged("EmployeeList");
}
}
private ListCollectionView _view;
public ICollectionView View
{
get { return this._view; }
}
private string _TextSearch;
public string TextSearch
{
get { return _TextSearch; }
set
{
_TextSearch = value;
OnPropertyChanged("TextSearch");
View.Refresh();
}
}
private List<Employee> GetEmployees()
{
var mylist = new List<Employee>();
for(int i = 1; i < 500; i++)
{
mylist.Add(new Employee() { FirstName = $"nummer{i}" });
}
return mylist;
}
}
public class Employee
{
string firstname;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html