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. Python: PS 滤镜--水波特效

    本文用 Python 实现 PS 滤镜中的 水波特效 import numpy as np from skimage import img_as_float import matplotlib.pyp ...

  2. [HAOI 2010] 计数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2425 [算法] 类似与数位动态规划的思想 , 用组合数学进行简单推导即可 时间复杂度 ...

  3. UItextFied 的属性

    网络学习笔记 在iPhone应用中通过UITextField填写信息时,经常出现出现自动更正输入信息.首字母大写等情况 尤其是在填写用户名时,这种本想提供便捷的功能反而让人感到特别麻烦 今天查了相关书 ...

  4. python学习笔记——Thread常用方法

    Thread对象中的一些方法: 以前说过多线程,用到threading模块中的Thread对象,其中的start和run方法比较熟悉了,start()是重载了Thread对象中的run方法,其实作用还 ...

  5. Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

    Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...

  6. DispatcherServlet详解

    1.1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring Io ...

  7. 2、css的存在形式及优先级

    一.优先级 简单可以理解为就近原则: <html lang="en"> <head> <meta charset="UTF-8"& ...

  8. 【网络爬虫】【java】微博爬虫(三):庖丁解牛——HTML结构分析与正则切分

    在上一篇文章中已经通过请求的url地址把html页面爬取下来了,这里分别以网易微博的html和新浪微博的html为例来分析如何提取微博数据. 一.网易微博解析 相比新浪微博的html结构,网易微博的比 ...

  9. WPF后台通知前台事件(ViewModelBase记录)

    版本1: 定义:public class ModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler P ...

  10. Swift异常处理

    在Swift里,抛出的异常必须继承Error这个协议.那么这个协议是什么呢? 按住command再点击Error我们可以看到, public protocol Error { } extension ...