Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows;
- using System.Windows.Controls;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Windows.Input;
- namespace DataGridTest5
- {
- /// <summary>
- /// Test case for sort issue in DataGrid
- /// a) Viewing:
- /// - start the application, the first level is shown automatically
- /// -> the sort order is OK
- /// - select level "Second"
- /// - select level "First"
- /// -> the sort order is not correct anymore
- /// b) Editing:
- /// - start the application, the first level is shown automatically
- /// -> the sort order is OK
- /// - edit names
- /// -> the DataGrid elements are sorted after each modification
- /// - select level "Second"
- /// - select level "First"
- /// -> the sort order is not correct anymore
- /// - edit names
- /// -> the DataGrid elements are not sorted, they stay at the same position
- /// </summary>
- public partial class MainWindow : Window
- {
- ObservableCollection<Level> levels;
- public MainWindow()
- {
- InitializeComponent();
- lstLevel.Loaded += new RoutedEventHandler(lstLevel_Loaded);
- levels = new ObservableCollection<Level> {
- new Level {
- Name = "First",
- Players = new ObservableCollection<User> {
- new User { Name = "Teddy", Age = "2" },
- new User { Name = "Avi", Age = "19" },
- new User { Name = "Mayia", Age = "30" },
- },
- },
- new Level {
- Name = "Second",
- Players = new ObservableCollection<User> {
- new User { Name = "Brian", Age = "20" },
- new User { Name = "Snoopy", Age = "4" }
- }
- }
- };
- lstLevel.ItemsSource = levels;
- }
- void lstLevel_Loaded(object sender, RoutedEventArgs e)
- {
- lstLevel.Focus();
- var item = lstLevel.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
- if (item != null)
- FocusManager.SetFocusedElement(this, item);
- }
- }
- /// <summary>
- /// Level class, items shown in the ListBox.
- /// </summary>
- public class Level : INotifyPropertyChanged
- {
- string name;
- public string Name
- {
- get { return name; }
- set
- {
- name = value;
- OnPropertyChanged(new PropertyChangedEventArgs("Name"));
- }
- }
- ObservableCollection<User> players;
- public ObservableCollection<User> Players
- {
- get { return players; }
- set
- {
- players = value;
- OnPropertyChanged(new PropertyChangedEventArgs("Players"));
- }
- }
- public Level()
- {
- }
- #region INotifyPropertyChanged Members
- public event PropertyChangedEventHandler PropertyChanged;
- public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
- {
- if (PropertyChanged != null)
- PropertyChanged(this, e);
- }
- #endregion
- }
- /// <summary>
- /// User class, items shown in the DataGrid.
- /// </summary>
- public class User : INotifyPropertyChanged
- {
- string name;
- public string Name
- {
- get { return name; }
- set
- {
- name = value;
- OnPropertyChanged(new PropertyChangedEventArgs("Name"));
- }
- }
- String age;
- public string Age
- {
- get { return age; }
- set
- {
- age = value;
- OnPropertyChanged(new PropertyChangedEventArgs("Age"));
- }
- }
- public User()
- {
- }
- #region INotifyPropertyChanged Members
- public event PropertyChangedEventHandler PropertyChanged;
- public virtual void OnPropertyChanged(PropertyChangedEventArgs e)
- {
- if (PropertyChanged != null)
- PropertyChanged(this, e);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement