Advertisement
Guest User

Untitled

a guest
Oct 31st, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. class PeopleViewModel
  2.     {
  3.         public ICollectionView SearchPeopleView { get; private set; }
  4.         public ObservableCollection<SearchPerson> SearchPeople { get; private set; }
  5.  
  6.         private string _searchString = "";
  7.  
  8.         public string SearchString
  9.         {
  10.             get { return _searchString; }
  11.             set
  12.             {
  13.                 _searchString = value;
  14.                 SearchPeopleView.Refresh();
  15.             }
  16.         }
  17.  
  18.  
  19.         public PeopleViewModel()
  20.         {
  21.             SearchPeople = MockData();
  22.             SearchPeopleView = CollectionViewSource.GetDefaultView(SearchPeople) as CollectionView;
  23.             SearchPeopleView.Filter = ViewFilter;
  24.         }
  25.  
  26.  
  27.         private ObservableCollection<SearchPerson> MockData()
  28.         {
  29.             return new ObservableCollection<SearchPerson>(
  30.                 Enumerable.Range(1, 10000)
  31.                            .Select(i => new SearchPerson() { FullName = $"{i} name" }));
  32.         }
  33.  
  34.         private bool ViewFilter(object obj)
  35.         {
  36.             var user = obj as SearchPerson;
  37.             return user != null && user.FullName.Contains(_searchString);
  38.         }
  39.     }
  40.  
  41.  
  42.     class SearchPerson
  43.     {
  44.         public string FullName { get; set; }
  45.     }
  46.  
  47. /*
  48. <!-- Xaml -->
  49. <Window x:Class="WpfApp1.MainWindow"
  50.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  51.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  52.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  53.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  54.         mc:Ignorable="d"
  55.         Title="MainWindow" Height="350" Width="525">
  56.     <DockPanel LastChildFill="True">
  57.         <TextBox DockPanel.Dock="Top" Text="{Binding SearchString, UpdateSourceTrigger=PropertyChanged, Delay=50}"/>
  58.         <DataGrid
  59.             ItemsSource="{Binding SearchPeopleView}">
  60.             <DataGrid.Columns>
  61.                 <DataGridTextColumn Binding="{Binding FullName}" Width="*"/>
  62.             </DataGrid.Columns>
  63.         </DataGrid>
  64.     </DockPanel>
  65. </Window>
  66. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement