Wpf ItemsSource排序

Posted on 2011-03-26 00:21  setter  阅读(1280)  评论(0)    收藏  举报
Code
对wpf 中的集合元素进行排序:         
         

 

1<TreeView Name="treeView1" Background="LightBlue"  Height="200">2</TreeView>

 

 1         private void InitData()
 2         {
 3             Staff staff = new Staff()
 4             {
 5                 Name = "1-off",
 6                 Status = Status.off
 7             };
 8             StaffTreeItem staffTreeItem = new StaffTreeItem(staff);
 9             this.treeView1.Items.Add(staffTreeItem);
10 
11             staff = new Staff()
12             {
13                 Name = "1-on",
14                 Status = Status.on
15             };
16             staffTreeItem = new StaffTreeItem(staff);
17             this.treeView1.Items.Add(staffTreeItem);
18 
19             staff = new Staff()
20             {
21                 Name = "1-away",
22                 Status = Status.away
23             };
24             staffTreeItem = new StaffTreeItem(staff);
25             this.treeView1.Items.Add(staffTreeItem);
26 

27         } 

 

1     public enum Status
2     {
3         on = 0,
4         away = 1,
5         off = 2

6     }  


  1.   public partial class StaffTreeItem : TreeViewItem
  2.     {
  3.         //public Staff staff;


  4.         public Staff staff
  5.         {
  6.             get { return (Staff)GetValue(staffProperty); }
  7.             set { SetValue(staffProperty, value); }
  8.         }

  9.         public static readonly DependencyProperty staffProperty =
  10.             DependencyProperty.Register("staff"typeof(Staff), typeof(StaffTreeItem), new UIPropertyMetadata(null));

  11.         public StaffTreeItem(Staff staff)
  12.         {
  13.             InitializeComponent();
  14.             this.staff = staff;
  15.             this.Header = staff.Name;
  16.             this.DataContext = staff;
  17.             this.Tag = staff;

  18.         }
  19.    } 

 

 

 


博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3