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. ...
随机推荐
- bind (ERROR 502): bind(0.0.0.0:9501) failed. Error: Address already in use [98] (端口被占用)
运行 swoole_server 服务报错显示端口被占用 解决思路: 1.用命令查看该端口 看是否存在 netstat -anp | grep 9501 2.如果存在 就用 kill对应端口号 ...
- codeforces 702B B. Powers of Two(水题)
题目链接: B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- BZOJ_1004_[HNOI2008]Cards_burnside+DP
BZOJ_1004_[HNOI2008]Cards_burnside+DP Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问 ...
- Tensorflow和Caffe 简介
TensorFlow TensorFlow 是相对高阶的机器学习库,用户可以方便地用它设计神经网络结构,而不必为了追求高效率的实现亲自写 C++或 CUDA 代码.它和 Theano 一样都支持自动求 ...
- FTP:文件传输协议(指令及响应代码)
文件传输协议(FTP)使得主机间可以共享文件. FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连接用于数据传输.控制连接使用类似 TELNET 协议在主机间交换命令 ...
- js联动
html: <!-- 省 --> <div class="col-sm-2"> <select name="p_id"> & ...
- 自适应文案提示框、无数据图片加载<IOS小组件>
非常感谢,帮助我的朋友们,谢谢你们. 该组件的编写仅仅用来不到4个小时,包括测试与修改bug.为他起名为AdaptivePromptDialogBox(就是自适应文案提示框): 呆毛地址:链接 < ...
- 简单易用"里程碑"、"时间轴"<iOS小组件>
非常感谢,帮助我的朋友们,谢谢你们.上次我的好朋友指出了我编码上(jwTextFiled工具组件)存在一些不规范问题,这次注意提高. 呆毛地址:https://github.com/NIUXINGJI ...
- 探究c++默认初始化
按照c++ primer 5th第40页的描述,如果定义变量时没有指定初值,则变量被默认初始化,此时变量被赋予了“默认值”. 根据变量定义的位置,分为两种情况: 1.定义于任何函数体之外的变量被初始化 ...
- Ubuntu环境下gedit以及vim的一些个简单配置
Gedit的配置: 参见 http://www.cnblogs.com/csulennon/p/4198054.html Gedit插件安装 Gedit快捷键 参见我的博客 添加快捷键 Ctrl + ...