This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Test case for sort issue in DataGrid

By: a guest on Aug 31st, 2010  |  syntax: C#  |  size: 4.41 KB  |  views: 84  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.Windows.Input;
  7.  
  8. namespace DataGridTest5
  9. {
  10.     /// <summary>
  11.     /// Test case for sort issue in DataGrid
  12.     /// a) Viewing:
  13.     /// - start the application, the first level is shown automatically
  14.     ///   -> the sort order is OK
  15.     /// - select level "Second"
  16.     /// - select level "First"
  17.     ///   -> the sort order is not correct anymore
  18.     /// b) Editing:
  19.     /// - start the application, the first level is shown automatically
  20.     ///   -> the sort order is OK
  21.     /// - edit names
  22.     ///   -> the DataGrid elements are sorted after each modification
  23.     /// - select level "Second"
  24.     /// - select level "First"
  25.     ///   -> the sort order is not correct anymore
  26.     /// - edit names
  27.     ///   -> the DataGrid elements are not sorted, they stay at the same position
  28.     /// </summary>
  29.     public partial class MainWindow : Window
  30.     {
  31.         ObservableCollection<Level> levels;
  32.  
  33.         public MainWindow()
  34.         {
  35.             InitializeComponent();
  36.             lstLevel.Loaded += new RoutedEventHandler(lstLevel_Loaded);
  37.             levels = new ObservableCollection<Level> {
  38.                 new Level {
  39.                     Name = "First",
  40.                     Players = new ObservableCollection<User> {
  41.                         new User { Name = "Teddy",  Age = "2" },
  42.                         new User { Name = "Avi",    Age = "19" },
  43.                         new User { Name = "Mayia",  Age = "30" },
  44.                     },
  45.                 },
  46.                 new Level {
  47.                     Name = "Second",
  48.                     Players = new ObservableCollection<User> {
  49.                                 new User { Name = "Brian",  Age = "20" },
  50.                                 new User { Name = "Snoopy", Age = "4" }
  51.                             }
  52.                     }
  53.             };
  54.             lstLevel.ItemsSource = levels;
  55.         }
  56.  
  57.         void lstLevel_Loaded(object sender, RoutedEventArgs e)
  58.         {
  59.             lstLevel.Focus();
  60.             var item = lstLevel.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
  61.             if (item != null)
  62.                 FocusManager.SetFocusedElement(this, item);
  63.         }
  64.     }
  65.  
  66.     /// <summary>
  67.     /// Level class, items shown in the ListBox.
  68.     /// </summary>
  69.     public class Level : INotifyPropertyChanged
  70.     {
  71.         string name;
  72.         public string Name
  73.         {
  74.             get { return name; }
  75.             set
  76.             {
  77.                 name = value;
  78.                 OnPropertyChanged(new PropertyChangedEventArgs("Name"));
  79.             }
  80.         }
  81.  
  82.         ObservableCollection<User> players;
  83.         public ObservableCollection<User> Players
  84.         {
  85.             get { return players; }
  86.             set
  87.             {
  88.                 players = value;
  89.                 OnPropertyChanged(new PropertyChangedEventArgs("Players"));
  90.             }
  91.         }
  92.  
  93.         public Level()
  94.         {
  95.         }
  96.  
  97.         #region INotifyPropertyChanged Members
  98.  
  99.         public event PropertyChangedEventHandler PropertyChanged;
  100.  
  101.         public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
  102.         {
  103.             if (PropertyChanged != null)
  104.                 PropertyChanged(this, e);
  105.         }
  106.  
  107.         #endregion
  108.     }
  109.    
  110.     /// <summary>
  111.     /// User class, items shown in the DataGrid.
  112.     /// </summary>
  113.     public class User : INotifyPropertyChanged
  114.     {
  115.         string name;
  116.         public string Name
  117.         {
  118.             get { return name; }
  119.             set
  120.             {
  121.                 name = value;
  122.                 OnPropertyChanged(new PropertyChangedEventArgs("Name"));
  123.             }
  124.         }
  125.  
  126.         String age;
  127.         public string Age
  128.         {
  129.             get { return age; }
  130.             set
  131.             {
  132.                 age = value;
  133.                 OnPropertyChanged(new PropertyChangedEventArgs("Age"));
  134.             }
  135.         }
  136.  
  137.         public User()
  138.         {
  139.         }
  140.  
  141.         #region INotifyPropertyChanged Members
  142.  
  143.         public event PropertyChangedEventHandler PropertyChanged;
  144.  
  145.         public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
  146.         {
  147.             if (PropertyChanged != null)
  148.                 PropertyChanged(this, e);
  149.         }
  150.  
  151.         #endregion
  152.     }
  153. }
clone this paste RAW Paste Data