ItemsControl属性GroupStyle
Grouping再ItemsControl源代码
1 public class ItemsControl : Control, IAddChild, IGeneratorHost 2 { 3 public static readonly DependencyProperty GroupStyleSelectorProperty; 4 private ObservableCollection<GroupStyle> _groupStyle = new ObservableCollection<GroupStyle>(); 5 6 public ObservableCollection<GroupStyle> GroupStyle 7 { 8 get 9 { 10 return this._groupStyle; 11 } 12 } 13 [Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), CustomCategory("Content")] 14 public GroupStyleSelector GroupStyleSelector 15 { 16 get 17 { 18 return (GroupStyleSelector)base.GetValue(ItemsControl.GroupStyleSelectorProperty); 19 } 20 set 21 { 22 base.SetValue(ItemsControl.GroupStyleSelectorProperty, value); 23 } 24 } 25 26 static ItemsControl() 27 { 28 ItemsControl.GroupStyleSelectorProperty = DependencyProperty.Register("GroupStyleSelector", typeof(GroupStyleSelector), typeof(ItemsControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(ItemsControl.OnGroupStyleSelectorChanged))); 29 } 30 31 private void CreateItemCollectionAndGenerator() 32 { 33 this._items = new ItemCollection(this); 34 this._itemContainerGenerator = new ItemContainerGenerator(this); 35 this._itemContainerGenerator.ChangeAlternationCount(); 36 ((INotifyCollectionChanged)this._items).CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnItemCollectionChanged); 37 if (this.IsInitPending) 38 { 39 this._items.BeginInit(); 40 } 41 else 42 { 43 if (base.IsInitialized) 44 { 45 this._items.BeginInit(); 46 this._items.EndInit(); 47 } 48 } 49 ((INotifyCollectionChanged)this._groupStyle).CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnGroupStyleChanged); 50 } 51 52 public bool ShouldSerializeGroupStyle() 53 { 54 return this.GroupStyle.Count > 0; 55 } 56 private void OnGroupStyleChanged(object sender, NotifyCollectionChangedEventArgs e) 57 { 58 if (this._itemContainerGenerator != null) 59 { 60 this._itemContainerGenerator.Refresh(); 61 } 62 } 63 private static void OnGroupStyleSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 64 { 65 ((ItemsControl)d).OnGroupStyleSelectorChanged((GroupStyleSelector)e.OldValue, (GroupStyleSelector)e.NewValue); 66 } 67 protected virtual void OnGroupStyleSelectorChanged(GroupStyleSelector oldGroupStyleSelector, GroupStyleSelector newGroupStyleSelector) 68 { 69 if (this._itemContainerGenerator != null) 70 { 71 this._itemContainerGenerator.Refresh(); 72 } 73 } 74 GroupStyle IGeneratorHost.GetGroupStyle(CollectionViewGroup group, int level) 75 { 76 GroupStyle groupStyle = null; 77 if (this.GroupStyleSelector != null) 78 { 79 groupStyle = this.GroupStyleSelector(group, level); 80 } 81 if (groupStyle == null) 82 { 83 if (level >= this.GroupStyle.Count) 84 { 85 level = this.GroupStyle.Count - 1; 86 } 87 if (level >= 0) 88 { 89 groupStyle = this.GroupStyle[level]; 90 } 91 } 92 return groupStyle; 93 } 94 }