我们在用到ItemsControl时,有时会用到分组,如ListBox,ListView,DataGrid。WPF的ItemsControl可以实现分组,是依托于GroupStyle,以ListBox为例,他的分组效果图为:

  以下为前台:

  1. <ListBox Name="lbMain">
  2. <ListBox.ItemTemplate>
  3. <DataTemplate>
  4. <StackPanel Orientation="Horizontal">
  5. <TextBlock Text="{Binding FileName}"
  6. Width="" />
  7. <TextBlock Text="{Binding AuthorName}"
  8. Width="" />
  9. <TextBlock Text="{Binding UpTime}"
  10. Width="" />
  11. </StackPanel>
  12. </DataTemplate>
  13. </ListBox.ItemTemplate>
  14. <ListBox.GroupStyle>
  15. <GroupStyle>
  16. <GroupStyle.ContainerStyle>
  17. <Style TargetType="{x:Type GroupItem}">
  18. <Setter Property="Template">
  19. <Setter.Value>
  20. <ControlTemplate TargetType="{x:Type GroupItem}">
  21. <Expander IsExpanded="True"
  22. ExpandDirection="Down">
  23. <Expander.Header>
  24. <StackPanel Orientation="Horizontal">
  25. <TextBlock Text="{Binding Path=Name}"
  26. VerticalAlignment="Center" />
  27. <TextBlock Text="{Binding Path=ItemCount, StringFormat=数量:{0}}"
  28. VerticalAlignment="Center"
  29. Margin="5,0,0,0" />
  30. <Button Content="Sale"
  31. Margin="5,0,0,0" />
  32. </StackPanel>
  33. </Expander.Header>
  34. <ItemsPresenter />
  35. </Expander>
  36. </ControlTemplate>
  37. </Setter.Value>
  38. </Setter>
  39. </Style>
  40. </GroupStyle.ContainerStyle>
  41. </GroupStyle>
  42. </ListBox.GroupStyle>
  43. </ListBox>

  从16行可以看出,GroupStyle定义的是控件内部样式,所以有人尝试在这里绑实体数据属性的话肯定是失败的,注意25行只能是Name,不管分组的属性叫什么名,这都只能是Name,我写了个Button在里面,如果想知道为什么只能是Name,写个Click处理,把Button的DataContext打印出来就什么都知道了。如果想在这里做更多的处理,比如进行一些负责的运算,可以写加转换器。

  这里只是弄了一个原始的Expander装载分组控件,需要美化可以另写样式。

  以下是后台:

  1. public class ModelFile
  2. {
  3. public string FileName { get; set; }
  4. public string AuthorName { get; set; }
  5. public string UpTime { get; set; }
  6. }
  7.  
  8. public partial class MainWindow : Window
  9. {
  10. public ObservableCollection<ModelFile> CollectionModelFile = new ObservableCollection<ModelFile>();
  11.  
  12. public MainWindow()
  13. {
  14. InitializeComponent();
  15.  
  16. CollectionModelFile.Add(new ModelFile() { FileName = "WPF进化史", AuthorName = "王鹏", UpTime = "2014-10-10" });
  17. CollectionModelFile.Add(new ModelFile() { FileName = "WPF概论", AuthorName = "大飞", UpTime = "2014-10-10" });
  18. CollectionModelFile.Add(new ModelFile() { FileName = "WPF之美", AuthorName = "小虫", UpTime = "2014-10-11" });
  19. CollectionModelFile.Add(new ModelFile() { FileName = "WPF之道", AuthorName = "青草", UpTime = "2014-11-11" });
  20. CollectionModelFile.Add(new ModelFile() { FileName = "WPF之禅", AuthorName = "得瑟鬼", UpTime = "2014-11-11" });
  21. CollectionModelFile.Add(new ModelFile() { FileName = "WPF入门", AuthorName = "今晚吃什么", UpTime = "2014-11-11" });
  22. CollectionModelFile.Add(new ModelFile() { FileName = "WPF神技", AuthorName = "无间道王二", UpTime = "2014-12-12" });
  23. CollectionModelFile.Add(new ModelFile() { FileName = "WPF不为人知之密", AuthorName = "星期八", UpTime = "2014-12-12" });
  24. CollectionModelFile.Add(new ModelFile() { FileName = "WPF之革命", AuthorName = "两把刀", UpTime = "2014-12-12" });
  25.  
  26. lbMain.ItemsSource = CollectionModelFile;
  27.  
  28. ICollectionView cv = CollectionViewSource.GetDefaultView(lbMain.ItemsSource);
  29. cv.GroupDescriptions.Add(new PropertyGroupDescription("UpTime"));
  30. }
  31. }

  重点是28、29行,有了这两句,ListBox就能准确得分组显示了,其他ItemsControl的分组类同。

  至此一个简单的ListBox分组显示就完成了,Demo已放群里,需要的可以下载来看。

WPF里ItemsControl的分组实现的更多相关文章

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

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

  2. WPF下Itemscontrol分组 样式

    原文 WPF下Itemscontrol分组 样式 <ItemsControl Grid.Row="1" DataContext="{Binding Layouts} ...

  3. WPF,ListView设置分组

    原文:WPF,ListView设置分组 今天遇到一个问题,就是在ListView中设置分组.想了很久在网上早了些资料作出一个例子. 分组字段也可以在后台中定义: CollectionView view ...

  4. 在WPF里实现计算器软件

    一.具体代码 类代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...

  5. WPF里的一些Effect特效

    原文:WPF里的一些Effect特效 Blend的特效都在Microsoft.Expression.Media.Effects里,用之前添加一下引用. 可以在前台选中对象后直接点击Effect新建一种 ...

  6. WPF中ItemsControl应用虚拟化时找到子元素的方法

    原文:WPF中ItemsControl应用虚拟化时找到子元素的方法  wpf的虚拟化技术会使UI的控件只初始化看的到的子元素, 而不是所有子元素都被初始化,这样会提高UI性能. 但是我们经常会遇到一个 ...

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

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

  8. WPF 自定义ItemsControl/ListBox/ListView控件样式

    一.前言 ItemsControl.ListBox.ListView这三种控件在WPF中都可作为列表信息展示控件.我们可以通过修改这三个控件的样式来展示我们的列表信息. 既然都是展示列表信息的控件,那 ...

  9. revit二次开发wpf里button按钮无法实现事务

    不能在revit提供的api外部使用事务,解决此方法, 1.把button里要实现的功能写到外部事件IExternalEventHandler中,注册外部事件,在button事件中.raise()使用 ...

随机推荐

  1. [UnityShader]点染队列、ZWrite和ZTest

    转载自:http://www.myexception.cn/mobile/1902628.html [UnityShader]渲染队列.ZWrite和ZTest 参考链接:http://blog.cs ...

  2. [原创]Centos7 安装配置ASP.NET Core+Nginx+Supervisor

    序言 此教程安装的都是最新版本的. 一键安装 有了这个神器,下面的教程就不用做了!只需运行几行代码,直接打开浏览器就可以访问! cd /home/ wget https://files.cnblogs ...

  3. --disable-column-names,--skip-column-names,--column-names=0

    --disable-column-names,--skip-column-names,--column-names=0

  4. [RDLC]心得整理(一)

    2014年在做项目的时候, 过用过RDLC, 之后便在没有使用过了. 最近又有项目使用rdlc, 感觉有些陌生,然后重新阅读了以前的笔记,想做一下整理. 常见问题: 1. 为什么rdlc报表出来的pd ...

  5. C#多线程Thread

    在项目中经常用到线程Thread,先做个简单记录,后面再完善下,方便以后参考.本人技术有限,如有不同见解之处,欢迎博友批评指正. 执行的线程Thread分无参数的,一个参数,多个参数的.直接看代码吧. ...

  6. 22 Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  7. JAVA对list集合进行排序Collections.sort()

    对一个集合中的对象进行排序,根据对象的某个指标的大小进行升序或降序排序.代码如下: // 进行降序排列 Collections.sort(list, new Comparator<ResultT ...

  8. python遇到IndentationError: unexpected indent

    由于tab和空格混用而导致的问题,解决办法如下: 在SubLime中View中的Identation中的Convert Indentitation to Spaces即可

  9. Mac 修改用户环境变量

    Mac 修改用户环境变量 sudo vim ~/.bash_profile

  10. U盘装CentOS6.4

    1.打开UltraISO,依次点击“文件”>“打开”,选择“CentOS-6.4-i386-bin-DVD1.iso”文件. 2.“启动”>“写入硬盘映像”,在“硬盘驱动器选择u盘”,写入 ...