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分组的更多相关文章

  1. WPF里ItemsControl的分组实现 --listbox 实现分组

    我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...

  2. WPF里ItemsControl的分组实现

    我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...

  3. 8.4Solr API使用(Result Grouping分组查询)

    转载请出自出处:http://eksliang.iteye.com/blog/2169458 一.概述 分组统计查询不同于分组统计(Facet),facet只是简单统计记录数,并不能为每组数据返回实际 ...

  4. 在XAML中为ItemsControl定义分组,适合mvvm绑定

    可以先参考一下这个文章: http://www.cnblogs.com/zoexia/archive/2014/11/30/4134012.html step0: 先展示一下最简陋的界面: 上图是一个 ...

  5. Dev Express Report 学习总结(二)关于如何使用Grouping分组

    对于所有的报表工具来说,基本上所有Grouping功能的都很相似.正如前面说到的,Group处于Page Header和Page Footer之间,同时又将Detail包括与其中. 下面还是通过一个例 ...

  6. 微软原文翻译:适用于.Net Core的WPF数据绑定概述

    原文链接,大部分是机器翻译,仅做了小部分修改.英.中文对照,看不懂的看英文. Data binding overview in WPF 2019/09/19 Data binding in Windo ...

  7. strom的使用02

    1.grouping分组策略 stream grouping就是用来定义一个stream应该如果分配给Bolts上面的多个Tasks. storm里面有6种类型的stream grouping: 1. ...

  8. Lucene查询语法详解

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

  9. (19)odoo中的javascript

    -----------更新日期15:17 2016-02-16 星期二-----------* 用到的js库   我们可以打开 addons/web/views/webclient_template. ...

随机推荐

  1. HDFS namenode 高可用(HA)搭建指南 QJM方式 ——本质是多个namenode选举master,用paxos实现一致性

    一.HDFS的高可用性 1.概述 本指南提供了一个HDFS的高可用性(HA)功能的概述,以及如何配置和管理HDFS高可用性(HA)集群.本文档假定读者具有对HDFS集群的组件和节点类型具有一定理解.有 ...

  2. uglifyjs2全局混淆

    从git克隆uglifyjs2源码后,进入目录: npm link 编译并安装uglifyjs2成功,就可以直接调用uglifyjs命令了.但是在进行全局混淆时出现了问题,虽然指定了文件topvar. ...

  3. Add GNOME to a CentOS Minimal Install

    by Jeff Hunter, Sr. Database Administrator Contents Introduction CentOS 6 About the Author Introduct ...

  4. BZOJ_3729_Gty的游戏_博弈论+splay+dfs序

    BZOJ_3729_Gty的游戏_博弈论+splay+dfs序 Description 某一天gty在与他的妹子玩游戏. 妹子提出一个游戏,给定一棵有根树,每个节点有一些石子,每次可以将不多于L的石子 ...

  5. [SHOI 2017] 寿司餐厅

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4873 [算法] 注意到题目中的限制条件可表述为 : 若选择区间[L , R] , 则 ...

  6. FTP:文件传输协议(指令及响应代码)

    文件传输协议(FTP)使得主机间可以共享文件. FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连接用于数据传输.控制连接使用类似 TELNET 协议在主机间交换命令 ...

  7. Robot Framework基础学习(一)

    Robot Framework语法学习: 一.变量的声明.赋值与使用 1.变量标识符:每个变量都可以用  变量标识符 ${变量名} 来表示. 2.变量声明:可以在TestSuite上点右键或者在Edi ...

  8. Flutter实战视频-移动电商-48.详细页_详情和评论的切换

    48.详细页_详情和评论的切换 增加切换的效果,我们主要是修改这个地方 这样我们的评论的内容就显示出来了 最终代码 details_web.dart import 'package:flutter/m ...

  9. bootstrap添加多个模态对话框支持

    bootstrap添加多个模态对话框支持 (2015-03-04 21:05:35) 转载▼ 标签: 房产   因为项目需要,在页面交互上要弹出多个dialog窗口,而bootstrap的modal支 ...

  10. js对象 数组Array详解 (参照MDN官网:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

    一:数组的创建方式: 1.采用直接量创建 var arr = [];//创建一个空数组 var arr2 = [1,2,3];//创建一个有三个元素的数组 2.采用构造函数创建 a.var arr1 ...