WPF里ItemsControl的分组实现
我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid。WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox为例,他的分组效果图为:
以下为前台:
- <ListBox Name="lbMain">
- <ListBox.ItemTemplate>
- <DataTemplate>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding FileName}"
- Width="" />
- <TextBlock Text="{Binding AuthorName}"
- Width="" />
- <TextBlock Text="{Binding UpTime}"
- Width="" />
- </StackPanel>
- </DataTemplate>
- </ListBox.ItemTemplate>
- <ListBox.GroupStyle>
- <GroupStyle>
- <GroupStyle.ContainerStyle>
- <Style TargetType="{x:Type GroupItem}">
- <Setter Property="Template">
- <Setter.Value>
- <ControlTemplate TargetType="{x:Type GroupItem}">
- <Expander IsExpanded="True"
- ExpandDirection="Down">
- <Expander.Header>
- <StackPanel Orientation="Horizontal">
- <TextBlock Text="{Binding Path=Name}"
- VerticalAlignment="Center" />
- <TextBlock Text="{Binding Path=ItemCount, StringFormat=数量:{0}}"
- VerticalAlignment="Center"
- Margin="5,0,0,0" />
- <Button Content="Sale"
- Margin="5,0,0,0" />
- </StackPanel>
- </Expander.Header>
- <ItemsPresenter />
- </Expander>
- </ControlTemplate>
- </Setter.Value>
- </Setter>
- </Style>
- </GroupStyle.ContainerStyle>
- </GroupStyle>
- </ListBox.GroupStyle>
- </ListBox>
从16行可以看出,GroupStyle定义的是控件内部样式,所以有人尝试在这里绑实体数据属性的话肯定是失败的,注意25行只能是Name,不管分组的属性叫什么名,这都只能是Name,我写了个Button在里面,如果想知道为什么只能是Name,写个Click处理,把Button的DataContext打印出来就什么都知道了。如果想在这里做更多的处理,比如进行一些负责的运算,可以写加转换器。
这里只是弄了一个原始的Expander装载分组控件,需要美化可以另写样式。
以下是后台:
- public class ModelFile
- {
- public string FileName { get; set; }
- public string AuthorName { get; set; }
- public string UpTime { get; set; }
- }
- public partial class MainWindow : Window
- {
- public ObservableCollection<ModelFile> CollectionModelFile = new ObservableCollection<ModelFile>();
- public MainWindow()
- {
- InitializeComponent();
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF进化史", AuthorName = "王鹏", UpTime = "2014-10-10" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF概论", AuthorName = "大飞", UpTime = "2014-10-10" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF之美", AuthorName = "小虫", UpTime = "2014-10-11" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF之道", AuthorName = "青草", UpTime = "2014-11-11" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF之禅", AuthorName = "得瑟鬼", UpTime = "2014-11-11" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF入门", AuthorName = "今晚吃什么", UpTime = "2014-11-11" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF神技", AuthorName = "无间道王二", UpTime = "2014-12-12" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF不为人知之密", AuthorName = "星期八", UpTime = "2014-12-12" });
- CollectionModelFile.Add(new ModelFile() { FileName = "WPF之革命", AuthorName = "两把刀", UpTime = "2014-12-12" });
- lbMain.ItemsSource = CollectionModelFile;
- ICollectionView cv = CollectionViewSource.GetDefaultView(lbMain.ItemsSource);
- cv.GroupDescriptions.Add(new PropertyGroupDescription("UpTime"));
- }
- }
重点是28、29行,有了这两句,ListBox就能准确得分组显示了,其他ItemsControl的分组类同。
至此一个简单的ListBox分组显示就完成了,Demo已放群里,需要的可以下载来看。
WPF里ItemsControl的分组实现的更多相关文章
- WPF里ItemsControl的分组实现 --listbox 实现分组
我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid.WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox ...
- WPF下Itemscontrol分组 样式
原文 WPF下Itemscontrol分组 样式 <ItemsControl Grid.Row="1" DataContext="{Binding Layouts} ...
- WPF,ListView设置分组
原文:WPF,ListView设置分组 今天遇到一个问题,就是在ListView中设置分组.想了很久在网上早了些资料作出一个例子. 分组字段也可以在后台中定义: CollectionView view ...
- 在WPF里实现计算器软件
一.具体代码 类代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...
- WPF里的一些Effect特效
原文:WPF里的一些Effect特效 Blend的特效都在Microsoft.Expression.Media.Effects里,用之前添加一下引用. 可以在前台选中对象后直接点击Effect新建一种 ...
- WPF中ItemsControl应用虚拟化时找到子元素的方法
原文:WPF中ItemsControl应用虚拟化时找到子元素的方法 wpf的虚拟化技术会使UI的控件只初始化看的到的子元素, 而不是所有子元素都被初始化,这样会提高UI性能. 但是我们经常会遇到一个 ...
- 在XAML中为ItemsControl定义分组,适合mvvm绑定
可以先参考一下这个文章: http://www.cnblogs.com/zoexia/archive/2014/11/30/4134012.html step0: 先展示一下最简陋的界面: 上图是一个 ...
- WPF 自定义ItemsControl/ListBox/ListView控件样式
一.前言 ItemsControl.ListBox.ListView这三种控件在WPF中都可作为列表信息展示控件.我们可以通过修改这三个控件的样式来展示我们的列表信息. 既然都是展示列表信息的控件,那 ...
- revit二次开发wpf里button按钮无法实现事务
不能在revit提供的api外部使用事务,解决此方法, 1.把button里要实现的功能写到外部事件IExternalEventHandler中,注册外部事件,在button事件中.raise()使用 ...
随机推荐
- [UnityShader]点染队列、ZWrite和ZTest
转载自:http://www.myexception.cn/mobile/1902628.html [UnityShader]渲染队列.ZWrite和ZTest 参考链接:http://blog.cs ...
- [原创]Centos7 安装配置ASP.NET Core+Nginx+Supervisor
序言 此教程安装的都是最新版本的. 一键安装 有了这个神器,下面的教程就不用做了!只需运行几行代码,直接打开浏览器就可以访问! cd /home/ wget https://files.cnblogs ...
- --disable-column-names,--skip-column-names,--column-names=0
--disable-column-names,--skip-column-names,--column-names=0
- [RDLC]心得整理(一)
2014年在做项目的时候, 过用过RDLC, 之后便在没有使用过了. 最近又有项目使用rdlc, 感觉有些陌生,然后重新阅读了以前的笔记,想做一下整理. 常见问题: 1. 为什么rdlc报表出来的pd ...
- C#多线程Thread
在项目中经常用到线程Thread,先做个简单记录,后面再完善下,方便以后参考.本人技术有限,如有不同见解之处,欢迎博友批评指正. 执行的线程Thread分无参数的,一个参数,多个参数的.直接看代码吧. ...
- 22 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- JAVA对list集合进行排序Collections.sort()
对一个集合中的对象进行排序,根据对象的某个指标的大小进行升序或降序排序.代码如下: // 进行降序排列 Collections.sort(list, new Comparator<ResultT ...
- python遇到IndentationError: unexpected indent
由于tab和空格混用而导致的问题,解决办法如下: 在SubLime中View中的Identation中的Convert Indentitation to Spaces即可
- Mac 修改用户环境变量
Mac 修改用户环境变量 sudo vim ~/.bash_profile
- U盘装CentOS6.4
1.打开UltraISO,依次点击“文件”>“打开”,选择“CentOS-6.4-i386-bin-DVD1.iso”文件. 2.“启动”>“写入硬盘映像”,在“硬盘驱动器选择u盘”,写入 ...