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查看视觉树

转载于:https://www.cnblogs.com/raohuagang/p/3627439.html

ItemsControl Grouping分组相关推荐

  1. WPF里ItemsControl的分组实现

    WPF里ItemsControl的分组实现 原文:WPF里ItemsControl的分组实现 我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid ...

  2. 【Storm】Spout的storm-starter及Grouping策略、并发度讲解、网站浏览量和用户数统计

    maven先安装好. 以下讲storm-starter的使用. 1.从github下载官方的storm-starter例子包,是maven工程, 地址 https://github.com/natha ...

  3. sql中聚合函数和分组函数_学习SQL:聚合函数

    sql中聚合函数和分组函数 SQL has many cool features and aggregate functions are definitely one of these feature ...

  4. 目标检测:Anchor-Free时代

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者:陀飞轮 自从去年8月CornerNet开始,Anchor-F ...

  5. 正则表达式教程手册、正则一点通(Chinar出品)

    C#语法之正则 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人! (拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享.心创新! ...

  6. Lucene查询语法详解

    Lucene查询 Lucene查询语法以可读的方式书写,然后使用JavaCC进行词法转换,转换成机器可识别的查询. 下面着重介绍下Lucene支持的查询: Terms词语查询 词语搜索,支持 单词 和 ...

  7. Hadoop/Spark相关面试问题总结

    Hadoop/Spark相关面试问题总结 面试回来之后把当中比較重要的问题记了下来写了个总结: (答案在后面) 1.简答说一下hadoop的map-reduce编程模型 2.hadoop的TextIn ...

  8. Hive中实现有序,有序concat拼接,有序集合,hive方法操作命令,与自带方法列表

    前言 记得以前用过这个函数,这次开发怎么都找不到了,不常用的原因,也是笔记没做好 方法一 GROUP_CONCAT(distinct id ORDER BY id DESC SEPARATOR '_' ...

  9. 利用反射操作bean的属性和方法

    今天在开发中碰到这样一个场景:当请求添加项目下的目录时,传过来的是一个IndexModel,这个Model里有关于这个目录字段的详细信息,包括基础报表,实时,漏斗等信息(这些字段类型都是boolean ...

  10. .NET Framework 4.8发布

    原文地址:https://devblogs.microsoft.com/dotnet/announcing-the-net-framework-4-8/ 我们很高兴地宣布今天发布.NET Framew ...

最新文章

  1. c++map的使用_mybatis源码 | mybatis插件及动态代理的使用
  2. 华为AR28-31配置光纤接入
  3. sqlmap自动扫描注入点_SQLmap JSON 格式的数据注入
  4. php另一个php的变量,php - PHP:如何更改依赖于另一个变量的变量? (新手资料) - SO中文参考 - www.soinside.com...
  5. input 模糊搜索
  6. Python学习——import语句导入模块顺序
  7. 性能测试组件CodeBenchmark V2发布
  8. 快乐学习 Ionic Framework+PhoneGap 手册1-3 {面板切换}
  9. Egret入门学习日记 --- 第八篇(书中 2.0~2.6节 内容)
  10. DockerCon 2017报告:企业在关注吗?
  11. mysql 存储过程 汉字取拼音或者首字母
  12. php获取当前域名的方法 如何获得域名
  13. STM32开发环境的搭建及使用——STM32CubeMX
  14. Maze环境以及DQN的实现
  15. CF-HW04-胡杰-16332054
  16. 有一个XxY的网格,一个机器人只能走格点且只能向右或向下走,要从左上角走到右下角。请设计一个算法,计算机器人有多少种走法。 给定两个正整数int x,int y,请返回机器人的走法数目。保证x+y小
  17. M1卡破解(自从学校升级系统之后,还准备在研究下)
  18. 抽象代数 04.01 群的生成元组
  19. 查看电脑内存个数、主频(工作频率)、容量、位宽等的方法总结
  20. ireport4.5在JVM中添加新字体解决方案(Font ‘標楷體‘ is not available to the JVM. See the Javadoc for more details.)

热门文章

  1. 刘知远老师的“灵魂发问”:关系抽取到底在乎什么?
  2. 百度AI快车道PaddleNLP实战营空降南京,11月9日技术大咖线下开讲
  3. 【爱你 祖国】细看我国智能无人机如何从无到有?都是被逼出来的!
  4. 排序算法之——希尔排序分析
  5. 从你王者荣耀爱玩的英雄类型,我就知道你关注哪些技术领域!
  6. 巧用“搜索”解决自学编程遇到的难题
  7. 微服务框架和工具大全
  8. 致Android开发者:APP 瘦身经验总结
  9. Linux: 系统设置与备份策略
  10. Spark:Spark 编程模型及快速入门