ItemsControl Grouping分组

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 }
View Code


定义数据模型

1     public class Data
2     {
3         public string Name { get; set; }
4         public string Value { get; set; }
5         public string Type { get; set; }
6     }

在设置GroupStyle
后台代码:

 1 public partial class MainWindow : Window
 2     {
 3         public MainWindow()
 4         {
 5             ObservableCollection<Data> data = new ObservableCollection<Data>();
 6             for (int i = 1; i <= 9; i += 2)
 7                 data.Add(new Data() { Name = i.ToString(), Type = "Odd" });
 8 
 9             for (int i = 0; i < 10; i += 2)
10                 data.Add(new Data() { Name = i.ToString(), Type = "Even" });
11 
12 
13             this.Resources.Add("data", data);
14             InitializeComponent();
15 
16             
17             ICollectionView vw = CollectionViewSource.GetDefaultView(data);
18             vw.GroupDescriptions.Add(new PropertyGroupDescription("Type"));
19             
20         }
21 
22     }
View Code

XAML代码:

 1 <Window x:Class="WpfCustomControl_One.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:local="clr-namespace:WpfCustomControl_One"
 5         Title="MainWindow" Height="350" Width="525">
 6     <Window.Resources>
 7     <DataTemplate DataType="{x:Type local:Data}">
 8         <StackPanel Orientation="Horizontal">
 9             <TextBlock Text="{Binding Value}"/>
10             <TextBlock Text="{Binding Type}" />
11         </StackPanel>
12     </DataTemplate>
13     </Window.Resources>
14     <Grid>
15         <Grid.RowDefinitions>
16             <RowDefinition/>
17             <RowDefinition/>
18         </Grid.RowDefinitions>
19         <ItemsControl Grid.Row="0" ItemsSource="{StaticResource data}">
20             <ItemsControl.GroupStyle>
21                 <GroupStyle>
22                     <GroupStyle.Panel>
23                         <ItemsPanelTemplate>
24                             <UniformGrid Columns="2"/>
25                         </ItemsPanelTemplate>
26                     </GroupStyle.Panel>
27                     <GroupStyle.HeaderTemplate>
28                         <DataTemplate>
29                             <Expander Header="Name"/>
30                         </DataTemplate>
31                     </GroupStyle.HeaderTemplate>
32                 </GroupStyle>
33             </ItemsControl.GroupStyle>
34         </ItemsControl>
35 
36         <StackPanel Grid.Row="1" Margin="5">
37             <ListBox ItemsSource="{StaticResource data}">
38                 <ListBox.GroupStyle>
39                     <GroupStyle>
40                         <GroupStyle.ContainerStyle>
41                             <Style TargetType="GroupItem">
42                                 <Setter Property="Template">
43                                     <Setter.Value>
44                                         <ControlTemplate>
45                                             <Expander Header="{Binding Name}">
46                                                 <ItemsPresenter/>
47                                             </Expander>
48                                         </ControlTemplate>
49                                     </Setter.Value>
50                                 </Setter>
51                             </Style>
52                         </GroupStyle.ContainerStyle>
53                     </GroupStyle>
54                 </ListBox.GroupStyle>
55             </ListBox>
56         </StackPanel>
57     </Grid>
58 </Window>

 Snoop查看视觉树

 

posted on 2014-03-27 01:39  饶华刚  阅读(1038)  评论(0编辑  收藏  举报

导航