Live Shaping

live shaping

Properties required for Live Shaping

  1. IsLiveFiltering = true;
  2. LiveFilteringProperties.Add("Value");
Example Live Sorting

In the following example, we have created a class named student. The class implements an INotifyPropertyChanged interface and is used for the Property’s value changes. Also a StudentCollection was created that populates the student information at runtime. On a Window_Loaded event we created an object of StudentCollection that is already populated with the student information and binds it with a dataGrid.

Also created a:
  1. DispatcherTimer dispTimer = new DispatcherTimer();
The dispatcher times that will update the Price property after a specific duration? On the click on the button the Sorting will start. It uses the following programming features:
  1. public class Student : INotifyPropertyChanged
  2. {
  3. int _StudentId;
  4. public int StudentId
  5. {
  6. get { return _StudentId; }
  7. set
  8. {
  9. _StudentId = value;
  10. OnPropertychanged("StudentId");
  11. }
  12. }
  13. string _StudentName;
  14. public string StudentName
  15. {
  16. get { return _StudentName; }
  17. set
  18. {
  19. _StudentName = value;
  20. OnPropertychanged("StudentName");
  21. }
  22. }
  23. string _StudentCity;
  24. public string StudentCity
  25. {
  26. get { return _StudentCity; }
  27. set
  28. {
  29. _StudentCity = value;
  30. OnPropertychanged("StudentCity");
  31. }
  32. }
  33. decimal _StudentFee;
  34. public decimal StudentFee
  35. {
  36. get { return _StudentFee; }
  37. set
  38. {
  39. _StudentFee = value;
  40. OnPropertychanged("StudentFee");
  41. }
  42. }
  43. public event PropertyChangedEventHandler PropertyChanged;
  44. private void OnPropertychanged(string pName)
  45. {
  46. if (PropertyChanged != null)
  47. {
  48. PropertyChanged(this, new PropertyChangedEventArgs(pName));
  49. }
  50. }
  51. }
  52. public class StudentCollection : ObservableCollection<Student>
  53. {
  54. public StudentCollection()
  55. {
  56. Add(new Student() { StudentId = 1,StudentName = "Pete Brown", StudentCity = "Chicago", StudentFee = 100 });
  57. Add(new Student() { StudentId = 2, StudentName = "Mikey Aurthor", StudentCity = "Phoenix", StudentFee = 150 });
  58. Add(new Student() { StudentId = 3, StudentName = "Dyna Mike", StudentCity = "Chicago", StudentFee = 800 });
  59. Add(new Student() { StudentId = 4, StudentName = "Rachel Dave", StudentCity = "Phoenix", StudentFee = 200 });
  60. Add(new Student() { StudentId = 5, StudentName = "Nikky Chre", StudentCity = "Phoenix", StudentFee = 180 });
  61. Add(new Student() { StudentId = 6, StudentName = "Karl Nas", StudentCity = "Chicago", StudentFee = 2200 });
  62. Add(new Student() { StudentId = 7, StudentName = "Bruce Wayne", StudentCity = "Seattle", StudentFee = 100 });
  63. Add(new Student() { StudentId = 9, StudentName = "Johan Surg", StudentCity = "Austin", StudentFee = 300 });
  64. Add(new Student() { StudentId = 10, StudentName = "Tony ", StudentCity = "Austin", StudentFee = 300 });
  65. Add(new Student() { StudentId = 11, StudentName = "Peterson J", StudentCity = "Seattle", StudentFee = 300 });
  66. Add(new Student() { StudentId = 12, StudentName = "Jack Russel", StudentCity = "Chicago", StudentFee = 300 });
  67. }
  68. }
XAML Design
  1. <Window x:Class="Wpf_Live_Shaping.LiveSorting"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title="LiveSorting" Height="400" Width="500" Loaded="Window_Loaded">
  5. <Grid>
  6. <DataGrid Name="DGData" Margin="10,40,10,10"></DataGrid>
  7. <Button Content="Start Sorting" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonStartSirting_Click"/>
  8. <Button Content="Stop Sorting" HorizontalAlignment="Left" Margin="100,10,0,0" VerticalAlignment="Top" Width="75" Click="ButtonStopSorting_Click"/>
  9. </Grid>
  10. </Window>
Code Behind
  1. public partial class LiveSorting : Window
  2. {
  3. DispatcherTimer dispTimer = new DispatcherTimer();
  4. ObservableCollection<Student> ListStudent = new ObservableCollection<Student>();
  5. public LiveSorting()
  6. {
  7. InitializeComponent();
  8. }
  9. private void Window_Loaded(object sender, RoutedEventArgs e)
  10. {
  11. var studentCollection = new StudentCollection();
  12. DGData.ItemsSource = studentCollection;
  13. Random random = new Random();
  14. var Students = new StudentCollection();
  15. foreach (var item in Students)
  16. {
  17. ListStudent.Add(new Student()
  18. {
  19. StudentId = item.StudentId,
  20. StudentName = item.StudentName,
  21. StudentCity = item.StudentCity,
  22. StudentFee = item.StudentFee
  23. });
  24. }
  25. DGData.ItemsSource = ListStudent;
  26. }
  27. void dispTimer_Tick(object sender, EventArgs e)
  28. {
  29. Random random = new Random();
  30. foreach (var pc in ListStudent)
  31. {
  32. pc.StudentFee += random.Next(100) - 5;
  33. }
  34. }
  35. private void ButtonStartSirting_Click(object sender, RoutedEventArgs e)
  36. {
  37. ICollectionView pView = CollectionViewSource.GetDefaultView(ListStudent);
  38. pView.SortDescriptions.Add(new SortDescription("StudentFee", ListSortDirection.Ascending));
  39. var productview = (ICollectionViewLiveShaping)CollectionViewSource.GetDefaultView(ListStudent);
  40. productview.IsLiveSorting = true;
  41. dispTimer.Interval = TimeSpan.FromMilliseconds(600);
  42. dispTimer.Tick += dispTimer_Tick;
  43. dispTimer.Start();
  44. }
  45. private void ButtonStopSorting_Click(object sender, RoutedEventArgs e)
  46. {
  47. dispTimer.Stop();
  48. }
  49. }
Result

live sorting

live sorting image

table

Example Live Filtering

In this example we will filter information that exists in a datagrid on the basis of StudentCity.

XAML Design
  1. <Grid>
  2. <Grid.RowDefinitions>
  3. <RowDefinition Height="100*"></RowDefinition>
  4. </Grid.RowDefinitions>
  5. <Grid.ColumnDefinitions>
  6. <ColumnDefinition Width="80*"></ColumnDefinition>
  7. <ColumnDefinition Width="30*"></ColumnDefinition>
  8. </Grid.ColumnDefinitions>
  9. <DataGrid Name="DGData" Margin="10,40,10,10" Grid.Row="0" Grid.Column="0"></DataGrid>
  10. <ListBox x:Name="lstCity"
  11. Height="160" Grid.Row="0" Grid.Column="1"
  12. SelectionChanged="lstCity_SelectionChanged"/>
  13. </Grid>
Code Behind
  1. public partial class LiveFilterWindow : Window
  2. {
  3. StudentCollection Students;
  4. List<string> Cities;
  5. public string CityName { get; set; }
  6. public LiveFilterWindow()
  7. {
  8. InitializeComponent();
  9. }
  10. private void Window_Loaded(object sender, RoutedEventArgs e)
  11. {
  12. Students = new StudentCollection();
  13. DGData.ItemsSource = Students;
  14. Cities = Students.Select(d => d.StudentCity).Distinct().ToList();
  15. lstCity.ItemsSource = Cities;
  16. }
  17. private void lstCity_SelectionChanged(object sender, SelectionChangedEventArgs e)
  18. {
  19. ListCollectionView studentView = CollectionViewSource.GetDefaultView(DGData.ItemsSource) as ListCollectionView;
  20. studentView.IsLiveFiltering = true;
  21. studentView.LiveFilteringProperties.Add("StudentCity");
  22. CityName = lstCity.SelectedItem.ToString();
  23. studentView.Filter = new Predicate<object>(IsCityFound);
  24. studentView.Refresh();
  25. }
  26. bool IsCityFound(object d)
  27. {
  28. bool result = false;
  29. Student std = d as Student;
  30. if (std.StudentCity == CityName)
  31. {
  32. result = true;
  33. }
  34. return result;
  35. }
  36. }
Output

chicago

seattle

chicago filter