Advertisement
KobeLin

Untitled

Sep 19th, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. [xaml]
  2. <Window.Resources>
  3. <CollectionViewSource x:Key="EmployeesSource" Source="{Binding Employees}" CollectionViewType="local:EmployeeCollectionView">
  4.  
  5. <CollectionViewSource.GroupDescriptions>
  6. <PropertyGroupDescription PropertyName="HireDate"/>
  7. </CollectionViewSource.GroupDescriptions>
  8.  
  9. </CollectionViewSource>
  10.  
  11. <DataTemplate x:Key="EmployeeTemplate">
  12. <StackPanel Orientation="Horizontal">
  13. <TextBlock Text="{Binding FirstName}" />
  14. <TextBlock Text="{Binding LastName}" />
  15. </StackPanel>
  16. </DataTemplate>
  17. </Window.Resources>
  18.  
  19. <Grid>
  20. <Grid.RowDefinitions>
  21. <RowDefinition Height="50"/>
  22. <RowDefinition Height="*"/>
  23. </Grid.RowDefinitions>
  24.  
  25. <StackPanel Grid.Row="0">
  26. <Button Name="addItem" Width="50" Height="50" Content="AddItem" HorizontalAlignment="Left" Click="addItem_Click"/>
  27. </StackPanel>
  28.  
  29. <ListBox Grid.Row="1" ItemsSource="{Binding Source={StaticResource EmployeesSource}}"
  30. ItemTemplate="{StaticResource EmployeeTemplate}"
  31. SelectionMode="Extended">
  32. <ListBox.GroupStyle>
  33. <x:Static Member="GroupStyle.Default"/>
  34. </ListBox.GroupStyle>
  35. </ListBox>
  36. </Grid>
  37.  
  38.  
  39. [C# code]
  40. public partial class Window1 : Window
  41. {
  42. //public IEnumerable<Employee> Employees { get; set; }
  43.  
  44. public ObservableCollection<Employee> Employees { get; set; }
  45.  
  46. public Window1()
  47. {
  48. InitializeComponent();
  49.  
  50. //初始化這邊可以加到UI上
  51. Employees = new ObservableCollection<Employee>
  52. {
  53. new Employee { FirstName = "FirstName1", LastName = "LastName1", HireDate = new DateTime(1970, 1, 1) },
  54. new Employee { FirstName = "FirstName2", LastName = "LastName2", HireDate = new DateTime(1970, 1, 1) },
  55. new Employee { FirstName = "FirstName3", LastName = "LastName3", HireDate = new DateTime(1980, 1, 1) },
  56. new Employee { FirstName = "FirstName4", LastName = "LastName4", HireDate = new DateTime(1980, 1, 1) },
  57. new Employee { FirstName = "FirstName5", LastName = "LastName5", HireDate = new DateTime(1980, 1, 1) },
  58. new Employee { FirstName = "FirstName6", LastName = "LastName6", HireDate = new DateTime(1990, 1, 1) },
  59. new Employee { FirstName = "FirstName7", LastName = "LastName7", HireDate = new DateTime(1990, 1, 1) },
  60. new Employee { FirstName = "FirstName8", LastName = "LastName8", HireDate = new DateTime(1990, 1, 1) },
  61. new Employee { FirstName = "FirstName9", LastName = "LastName9", HireDate = new DateTime(1990, 1, 1) },
  62. };
  63. DataContext = this;
  64. }
  65.  
  66. private void addItem_Click(object sender, RoutedEventArgs e)
  67. {
  68. //有增加進去, 但是UI不會新增這一筆1983的資料
  69. Employees.Add(new Employee {
  70. FirstName = "FirstName10",
  71. LastName = "LastName10",
  72. HireDate = new DateTime(1983, 1, 1)
  73. });
  74. }
  75. }
  76.  
  77. class EmployeeCollectionView : ListCollectionView
  78. {
  79. private readonly IList<EmployeeCollectionViewGroup> _groups;
  80. public override ReadOnlyObservableCollection<object> Groups
  81. {
  82. get { return new ReadOnlyObservableCollection<object>(new ObservableCollection<object>(_groups)); }
  83. }
  84.  
  85.  
  86. public EmployeeCollectionView(IList list)
  87. : base(list)
  88. {
  89. _groups = list
  90. .OfType<Employee>()
  91. .GroupBy(x => x.HireDate)
  92. .Select(x => new EmployeeCollectionViewGroup(x.Key, x))
  93. .ToList();
  94. }
  95.  
  96.  
  97. }
  98.  
  99. class EmployeeCollectionViewGroup : CollectionViewGroup
  100. {
  101. public EmployeeCollectionViewGroup(object name, IEnumerable<object> protectedItems)
  102. : base(name)
  103. {
  104. foreach (var protectedItem in protectedItems)
  105. ProtectedItems.Add(protectedItem);
  106. }
  107.  
  108. public override bool IsBottomLevel
  109. {
  110. get { return true; }
  111. }
  112.  
  113. //新增屬性
  114. public bool IsDeleting { get; set; }
  115. }
  116.  
  117. public class Employee : INotifyPropertyChanged
  118. {
  119. public event PropertyChangedEventHandler PropertyChanged;
  120. private string m_FirstName;
  121. public string FirstName
  122. {
  123. get { return m_FirstName; }
  124. set
  125. {
  126. m_FirstName = value;
  127. if (PropertyChanged != null)
  128. PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
  129. }
  130.  
  131. }
  132. private string m_LastName;
  133. public string LastName
  134. {
  135. get { return m_LastName; }
  136. set
  137. {
  138. m_LastName = value;
  139. if (PropertyChanged != null)
  140. PropertyChanged(this, new PropertyChangedEventArgs("LastName"));
  141. }
  142. }
  143.  
  144. private DateTime m_HireDate;
  145. public DateTime HireDate
  146. {
  147. get { return m_HireDate; }
  148. set
  149. {
  150. m_HireDate = value;
  151. if (PropertyChanged != null)
  152. PropertyChanged(this, new PropertyChangedEventArgs("HireDate"));
  153. }
  154. }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement