ItemsControl Grouping分组
ItemsControl属性GroupStyle
Grouping再ItemsControl源代码
public class ItemsControl : Control, IAddChild, IGeneratorHost
{
public static readonly DependencyProperty GroupStyleSelectorProperty;
private ObservableCollection<GroupStyle> _groupStyle = new ObservableCollection<GroupStyle>(); public ObservableCollection<GroupStyle> GroupStyle
{
get
{
return this._groupStyle;
}
}
[Bindable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), CustomCategory("Content")]
public GroupStyleSelector GroupStyleSelector
{
get
{
return (GroupStyleSelector)base.GetValue(ItemsControl.GroupStyleSelectorProperty);
}
set
{
base.SetValue(ItemsControl.GroupStyleSelectorProperty, value);
}
} static ItemsControl()
{
ItemsControl.GroupStyleSelectorProperty = DependencyProperty.Register("GroupStyleSelector", typeof(GroupStyleSelector), typeof(ItemsControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(ItemsControl.OnGroupStyleSelectorChanged)));
} private void CreateItemCollectionAndGenerator()
{
this._items = new ItemCollection(this);
this._itemContainerGenerator = new ItemContainerGenerator(this);
this._itemContainerGenerator.ChangeAlternationCount();
((INotifyCollectionChanged)this._items).CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnItemCollectionChanged);
if (this.IsInitPending)
{
this._items.BeginInit();
}
else
{
if (base.IsInitialized)
{
this._items.BeginInit();
this._items.EndInit();
}
}
((INotifyCollectionChanged)this._groupStyle).CollectionChanged += new NotifyCollectionChangedEventHandler(this.OnGroupStyleChanged);
} public bool ShouldSerializeGroupStyle()
{
return this.GroupStyle.Count > ;
}
private void OnGroupStyleChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (this._itemContainerGenerator != null)
{
this._itemContainerGenerator.Refresh();
}
}
private static void OnGroupStyleSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((ItemsControl)d).OnGroupStyleSelectorChanged((GroupStyleSelector)e.OldValue, (GroupStyleSelector)e.NewValue);
}
protected virtual void OnGroupStyleSelectorChanged(GroupStyleSelector oldGroupStyleSelector, GroupStyleSelector newGroupStyleSelector)
{
if (this._itemContainerGenerator != null)
{
this._itemContainerGenerator.Refresh();
}
}
GroupStyle IGeneratorHost.GetGroupStyle(CollectionViewGroup group, int level)
{
GroupStyle groupStyle = null;
if (this.GroupStyleSelector != null)
{
groupStyle = this.GroupStyleSelector(group, level);
}
if (groupStyle == null)
{
if (level >= this.GroupStyle.Count)
{
level = this.GroupStyle.Count - ;
}
if (level >= )
{
groupStyle = this.GroupStyle[level];
}
}
return groupStyle;
}
}
定义数据模型
public class Data
{
public string Name { get; set; }
public string Value { get; set; }
public string Type { get; set; }
}
在设置GroupStyle
后台代码:
public partial class MainWindow : Window
{
public MainWindow()
{
ObservableCollection<Data> data = new ObservableCollection<Data>();
for (int i = ; i <= ; i += )
data.Add(new Data() { Name = i.ToString(), Type = "Odd" }); for (int i = ; i < ; i += )
data.Add(new Data() { Name = i.ToString(), Type = "Even" }); this.Resources.Add("data", data);
InitializeComponent(); ICollectionView vw = CollectionViewSource.GetDefaultView(data);
vw.GroupDescriptions.Add(new PropertyGroupDescription("Type")); } }
XAML代码:
<Window x:Class="WpfCustomControl_One.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControl_One"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Data}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Value}"/>
<TextBlock Text="{Binding Type}" />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ItemsControl Grid.Row="" ItemsSource="{StaticResource data}">
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<UniformGrid Columns=""/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<Expander Header="Name"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ItemsControl.GroupStyle>
</ItemsControl> <StackPanel Grid.Row="" Margin="">
<ListBox ItemsSource="{StaticResource data}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="GroupItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}">
<ItemsPresenter/>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListBox.GroupStyle>
</ListBox>
</StackPanel>
</Grid>
</Window>

Snoop查看视觉树

ItemsControl Grouping分组的更多相关文章
- WPF里ItemsControl的分组实现 --listbox 实现分组
我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...
- WPF里ItemsControl的分组实现
我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...
- 8.4Solr API使用(Result Grouping分组查询)
转载请出自出处:http://eksliang.iteye.com/blog/2169458 一.概述 分组统计查询不同于分组统计(Facet),facet只是简单统计记录数,并不能为每组数据返回实际 ...
- 在XAML中为ItemsControl定义分组,适合mvvm绑定
可以先参考一下这个文章: http://www.cnblogs.com/zoexia/archive/2014/11/30/4134012.html step0: 先展示一下最简陋的界面: 上图是一个 ...
- Dev Express Report 学习总结(二)关于如何使用Grouping分组
对于所有的报表工具来说,基本上所有Grouping功能的都很相似.正如前面说到的,Group处于Page Header和Page Footer之间,同时又将Detail包括与其中. 下面还是通过一个例 ...
- 微软原文翻译:适用于.Net Core的WPF数据绑定概述
原文链接,大部分是机器翻译,仅做了小部分修改.英.中文对照,看不懂的看英文. Data binding overview in WPF 2019/09/19 Data binding in Windo ...
- strom的使用02
1.grouping分组策略 stream grouping就是用来定义一个stream应该如果分配给Bolts上面的多个Tasks. storm里面有6种类型的stream grouping: 1. ...
- Lucene查询语法详解
Lucene查询 Lucene查询语法以可读的方式书写,然后使用JavaCC进行词法转换,转换成机器可识别的查询. 下面着重介绍下Lucene支持的查询: Terms词语查询 词语搜索,支持 单词 和 ...
- (19)odoo中的javascript
-----------更新日期15:17 2016-02-16 星期二-----------* 用到的js库 我们可以打开 addons/web/views/webclient_template. ...
随机推荐
- Floyd算法(弗洛伊德算法) 百度百科
核心代码 for(int k=1; k<=NODE; ++k)//对于每一个中转点 for(int i=0; i<=NODE; ++i)//枚举源点 for(int j=0; j<= ...
- Rsync+Sersync同步
Rsync+Sersync同步特点: (1):sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字:(2):rsync在同步的时候,只同步发生变化的这 ...
- ip策略路由
ip route 只是基于目的地址的路由选择 ip rule 路由策略,控制路由选择,可根据源地址,源IP等进行路由选择 路由策略由选择符合操作组成 ip rule add 添加策略 ip r ...
- vue-cli脚手架搭建Vue.js项目
前提条件: 一.node.js 下载 https://nodejs.org/zh-cn/download/ 二.webpack 安装 npm install webpack -g PS:-g 就是 ...
- 编译生成的h.gch文件是什么鬼?
所谓预编译头,就是把头文件事先编译成一种二进制的中间格式,供后续的编译过程使用.GCC编译头文件后的中间文件是*.gch. 如何将头文件编译为.gch文件呢?用g++编译,格式: g++ xxx.h ...
- lsyncd实时同步搭建指南——取代rsync+inotify
1. 几大实时同步工具比较 1.1 inotify + rsync 最近一直在寻求生产服务服务器上的同步替代方案,原先使用的是inotify + rsync,但随着文件数量的增大到100W+,目录下的 ...
- C语言指针入门知识
C语言指针往往是C语言学习过程中最困难的地方, 最近重新理解了一下C语言的指针知识, 在此整理一下, 如果有错误请留言指正. 对于刚入门的人来说, 指针涉及方方面面, 从简单的数组到结构体, 都会用到 ...
- 《Java多线程编程核心技术》读后感(一)
1.继承Thread package First; public class MyThread extends Thread { public void run() { super.run(); Sy ...
- c++函数模板二栈实现
1 没有使用模板的栈实现 #include <iostream> #include <string> using namespace std; class Stack { pu ...
- Grid++Report应用(引入项目中)
1.将Grid++Report安装文件中(\WebSamples\asp.net(csharp)\App_Code)的ReportData.cs,MssqlReportData.cs两个文件复制到自己 ...