[源码下载]

背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组

作者:webabcd

介绍
背水一战 Windows 10 之 控件(集合类 - ItemsControl)

  • 项模板选择器
  • 数据分组

示例
1、ItemsControl 的项模板选择器
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo3.xaml

  1. <Page
  2. x:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.ItemsControlDemo3"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.CollectionControl.ItemsControlDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d"
  9.  
  10. xmlns:common="using:Windows10.Common">
  11.  
  12. <Page.Resources>
  13. <!--
  14. DataTemplate - 数据模板
  15. -->
  16. <DataTemplate x:DataType="common:Employee" x:Key="DataTemplateMale">
  17. <Grid Background="Blue">
  18. <TextBlock Text="{x:Bind Name}" />
  19. </Grid>
  20. </DataTemplate>
  21. <DataTemplate x:DataType="common:Employee" x:Key="DataTemplateFemale">
  22. <Grid Background="Pink">
  23. <TextBlock Text="{x:Bind Name}" />
  24. </Grid>
  25. </DataTemplate>
  26.  
  27. <!--
  28. 自定义数据模板选择器(参见 code-behind 中的代码)
  29. -->
  30. <local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
  31. DataTemplate1="{StaticResource DataTemplateMale}"
  32. DataTemplate2="{StaticResource DataTemplateFemale}" />
  33. </Page.Resources>
  34.  
  35. <Grid Background="Transparent">
  36. <StackPanel Margin="10 0 10 10" Orientation="Horizontal">
  37.  
  38. <!--
  39. ItemsControl - 集合控件
  40. ItemTemplateSelector - 每个数据项的数据模板选择器(如果指定了 ItemTemplate 则此配置无效)
  41. -->
  42. <ListView Name="itemsControl" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top"
  43. ItemsSource="{x:Bind Employees}"
  44. ItemTemplateSelector="{StaticResource MyDataTemplateSelector}">
  45.  
  46. </ListView>
  47.  
  48. </StackPanel>
  49. </Grid>
  50. </Page>

Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo3.xaml.cs

  1. /*
  2. * ItemsControl - 集合控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
  3. *
  4. *
  5. * 本例用于演示 ItemsControl 如何通过 item 的不同而使用不同的数据模板
  6. */
  7.  
  8. using System.Collections.ObjectModel;
  9. using Windows.UI.Xaml;
  10. using Windows.UI.Xaml.Controls;
  11. using Windows10.Common;
  12.  
  13. namespace Windows10.Controls.CollectionControl.ItemsControlDemo
  14. {
  15. public sealed partial class ItemsControlDemo3 : Page
  16. {
  17. public ObservableCollection<Employee> Employees { get; set; } = TestData.GetEmployees();
  18.  
  19. public ItemsControlDemo3()
  20. {
  21. this.InitializeComponent();
  22. }
  23. }
  24.  
  25. // 自定义 DataTemplateSelector(数据模板选择器)
  26. // 可以实现在 runtime 时,根据 item 的不同选择不同的数据模板
  27. public class MyDataTemplateSelector : DataTemplateSelector
  28. {
  29. // 数据模板 1(配置在 xaml 端)
  30. public DataTemplate DataTemplate1 { get; set; }
  31.  
  32. // 数据模板 2(配置在 xaml 端)
  33. public DataTemplate DataTemplate2 { get; set; }
  34.  
  35. // 根据 item 的数据的不同,指定的不同的模板
  36. protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
  37. {
  38. var employee = item as Employee;
  39. if (employee == null || employee.IsMale)
  40. return DataTemplate1; // 男员工用数据模板 1
  41. return DataTemplate2; // 女员工用数据模板 2
  42.  
  43. // 如果想直接返回指定的资源也是可以的(但是不灵活),类似:return (DataTemplate)Application.Current.Resources["DataTemplateMale"];
  44. }
  45. }
  46. }

2、ItemsControl 的数据分组
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo4.xaml

  1. <Page
  2. x:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.ItemsControlDemo4"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Controls.CollectionControl.ItemsControlDemo"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Page.Resources>
  11.  
  12. <!--
  13. GroupStyle - 组样式
  14. HidesIfEmpty - 空组是否隐藏
  15. HeaderContainerStyle - 组标题的容器样式
  16. HeaderTemplate - 组标题的模板
  17. HeaderTemplateSelector - 组标题的模板选择器
  18.  
  19. 注:
  20. ListView 的 Group 的 HeaderContainer 是 ListViewHeaderItem, GridView 的 Group 的 HeaderContainer 是 GridViewHeaderItem
  21. ListViewHeaderItem 和 GridViewHeaderItem 均继承自 ListViewBaseHeaderItem, ListViewBaseHeaderItem 继承自 ContentControl
  22. -->
  23. <GroupStyle x:Key="GroupStyle1" HeaderTemplate="{StaticResource DataTemplateGroupHeader}">
  24. <GroupStyle.HeaderContainerStyle>
  25. <Style TargetType="ListViewHeaderItem">
  26. <Setter Property="Background" Value="Blue" />
  27. </Style>
  28. </GroupStyle.HeaderContainerStyle>
  29. </GroupStyle>
  30. <GroupStyle x:Key="GroupStyle2" HeaderTemplate="{StaticResource DataTemplateGroupHeader}">
  31. <GroupStyle.HeaderContainerStyle>
  32. <Style TargetType="ListViewHeaderItem">
  33. <Setter Property="Background" Value="Orange" />
  34. </Style>
  35. </GroupStyle.HeaderContainerStyle>
  36. </GroupStyle>
  37.  
  38. <DataTemplate x:Key="DataTemplateGroupHeader">
  39. <TextBlock Text="{Binding Title}" />
  40. </DataTemplate>
  41.  
  42. <!--
  43. 自定义 GroupStyle 选择器(参见 code-behind 中的代码)
  44. -->
  45. <local:MyGroupStyleSelector x:Key="MyGroupStyleSelector"
  46. GroupStyle1="{StaticResource GroupStyle1}"
  47. GroupStyle2="{StaticResource GroupStyle2}" />
  48. </Page.Resources>
  49.  
  50. <Grid Background="Transparent">
  51. <StackPanel Margin="10 0 10 10">
  52.  
  53. <!--
  54. ItemsControl - 集合控件
  55. ItemsPanel - 用于指定 items 的布局控件,任何 Panel 类型的布局控件均可,所有 items 将在 Panel 内显示(Panel 是所有 items 的容器)
  56. 给 ItemsControl 用的,可虚拟化的布局控件有:ItemsStackPanel, ItemsWrapGrid, VirtualizingStackPanel, WrapGrid. 请参见:/Controls/CollectionControl/ItemsControlDemo/LayoutControl/
  57. GroupStyle - 组样式
  58. GroupStyleSelector - 组样式选择器
  59. -->
  60. <ListView Name="listView" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" ItemsSource="{x:Bind MyData.View}"
  61. GroupStyleSelector="{StaticResource MyGroupStyleSelector}" SelectionChanged="listView_SelectionChanged">
  62. <!--
  63. <ListView.GroupStyle>
  64. <GroupStyle>
  65. <GroupStyle.HeaderTemplate>
  66. <DataTemplate>
  67. <TextBlock Text="{Binding Title}" />
  68. </DataTemplate>
  69. </GroupStyle.HeaderTemplate>
  70. </GroupStyle>
  71. </ListView.GroupStyle>
  72. -->
  73. <ListView.ItemTemplate>
  74. <DataTemplate>
  75. <TextBlock Text="{Binding Title}" Foreground="Purple" />
  76. </DataTemplate>
  77. </ListView.ItemTemplate>
  78. <ListView.ItemsPanel>
  79. <ItemsPanelTemplate>
  80. <ItemsStackPanel />
  81. </ItemsPanelTemplate>
  82. </ListView.ItemsPanel>
  83. </ListView>
  84.  
  85. <TextBlock Name="lblMsg" Margin="5" />
  86.  
  87. </StackPanel>
  88. </Grid>
  89. </Page>

Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo4.xaml.cs

  1. /*
  2. * ItemsControl - 集合控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
  3. * IsGrouping - 当前 ItemsControl 显示的是否是分组数据(只读)
  4. * DependencyObject GroupHeaderContainerFromItemContainer(DependencyObject itemContainer) - 获取指定 ItemContainer 的 HeaderContainer
  5. *
  6. *
  7. * 本例用于演示如何通过 ItemsControl 显示分组数据
  8. *
  9. * 注:本例是用 ListView 来演示数据分组的,用 GridView 做数据分组的示例请参见 /Index.xaml
  10. */
  11.  
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Xml.Linq;
  16. using Windows.UI.Popups;
  17. using Windows.UI.Xaml;
  18. using Windows.UI.Xaml.Controls;
  19. using Windows.UI.Xaml.Data;
  20. using Windows10.Common;
  21.  
  22. namespace Windows10.Controls.CollectionControl.ItemsControlDemo
  23. {
  24. public sealed partial class ItemsControlDemo4 : Page
  25. {
  26. public CollectionViewSource MyData
  27. {
  28. get
  29. {
  30. XElement root = XElement.Load("SiteMap.xml");
  31. var items = LoadData(root);
  32.  
  33. // 构造数据源
  34. CollectionViewSource source = new CollectionViewSource();
  35. source.IsSourceGrouped = true;
  36. source.Source = items;
  37. source.ItemsPath = new PropertyPath("Items");
  38.  
  39. return source;
  40. }
  41. }
  42.  
  43. public ItemsControlDemo4()
  44. {
  45. this.InitializeComponent();
  46.  
  47. this.Loaded += ItemsControlDemo4_Loaded;
  48. }
  49.  
  50. private void ItemsControlDemo4_Loaded(object sender, RoutedEventArgs e)
  51. {
  52. lblMsg.Text = "IsGrouping: " + listView.IsGrouping.ToString();
  53. }
  54.  
  55. private async void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
  56. {
  57. if (e.AddedItems.Count > && listView.ContainerFromItem(e.AddedItems[]) != null)
  58. {
  59. // 获取选中数据的 HeaderContainer
  60. ListViewHeaderItem headerContainer = listView.GroupHeaderContainerFromItemContainer(listView.ContainerFromItem(e.AddedItems[])) as ListViewHeaderItem;
  61.  
  62. NavigationModel headerNavigationModel = headerContainer.Content as NavigationModel;
  63. await new MessageDialog($"header: {headerNavigationModel.Title}").ShowAsync();
  64. }
  65. }
  66.  
  67. // 解析 xml 数据
  68. private List<NavigationModel> LoadData(XElement root)
  69. {
  70. if (root == null)
  71. return null;
  72.  
  73. var items = from n in root.Elements("node")
  74. select new NavigationModel
  75. {
  76. Title = (string)n.Attribute("title"),
  77. Url = (string)n.Attribute("url"),
  78. Items = LoadData(n)
  79. };
  80.  
  81. return items.ToList();
  82. }
  83. }
  84.  
  85. // 自定义 MyGroupStyleSelector(GroupStyle 选择器)
  86. // 可以实现在 runtime 时,根据 group 的不同选择不同的 GroupStyle
  87. public class MyGroupStyleSelector : GroupStyleSelector
  88. {
  89. static bool temp = false;
  90.  
  91. // GroupStyle 1(配置在 xaml 端)
  92. public GroupStyle GroupStyle1 { get; set; }
  93.  
  94. // GroupStyle 2(配置在 xaml 端)
  95. public GroupStyle GroupStyle2 { get; set; }
  96.  
  97. protected override GroupStyle SelectGroupStyleCore(object group, uint level)
  98. {
  99. // 我这里测试,group 要么是 null 要么是 DependencyObject,level 总是 0
  100.  
  101. // 利用这个变量,来演示如何让不同的 group 使用不同的 GroupStyle
  102. temp ^= true;
  103. if (temp)
  104. return GroupStyle1;
  105. return GroupStyle2;
  106.  
  107. // 如果想直接返回指定的资源也是可以的(但是不灵活),类似:return (GroupStyle)Application.Current.Resources["GroupStyle1"];
  108. }
  109. }
  110. }

OK
[源码下载]

背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组的更多相关文章

  1. 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresenter, ListViewItemPresenter

    [源码下载] 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresen ...

  2. 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter

    [源码下载] 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter 作者:weba ...

  3. 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid

    [源码下载] 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid 作者:webabc ...

  4. 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingStackPanel, WrapGrid

    [源码下载] 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingS ...

  5. 背水一战 Windows 10 (49) - 控件(集合类): Pivot, Hub

    [源码下载] 背水一战 Windows 10 (49) - 控件(集合类): Pivot, Hub 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) Pivot Hub 示 ...

  6. 背水一战 Windows 10 (48) - 控件(集合类): FlipView

    [源码下载] 背水一战 Windows 10 (48) - 控件(集合类): FlipView 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) FlipView 示例Fl ...

  7. 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation

    [源码下载] 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation 作者:webabcd 介绍背水一战 Wind ...

  8. 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项

    [源码下载] 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合 ...

  9. 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制

    [源码下载] 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制 作者:webabcd 介绍背水一战 Windows 10 之 控件(集 ...

随机推荐

  1. mybatis 复杂传参

    1基本传参数 Public User selectUserWithCon(@param(“userName”)String  name,@param(“userArea”)String area); ...

  2. idea配置springBoot项目热加载

    1.在application.properties中禁用模板引擎缓存 比如freemarker:spring.freemarker.cache=false 2.在pom.xml中添加依赖 <de ...

  3. 【Linux】CentOS 7.2 安装 MySQL 5.7.21 解压版

    安装环境/工具 1.Linux(CentOS 7.2版) 2.mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz 安装步骤 1.下载mysql解压版(mysql-5. ...

  4. C语言基础第四次作业

    题目7-2,九九乘法表 1.实验代码: #include<stdio.h> int main() { int N, i, j, q; scanf("%d",&N ...

  5. linux_开启mysql服务

    想要连接mysql的时候必须先开启mysql的服务 service mysqld start mysql -u root -p 输入密码

  6. 使用delphi 10.2 开发linux 上的Daemon

    delphi 10.2 支持linux, 而且官方只是支持命令行编程,目地就是做linux 服务器端的开发. 既然是做linux服务器端的开发,那么普通的命令行运行程序,然后等待开一个黑窗口的方式就 ...

  7. java笔记--问题总结

    1. 垃圾回收算法 标记-清除算法 标记-清除算法是最基本的算法,和他的名字一样,分为两个步骤,一个步骤是标记需要回收的对象.在标记完成后统一回收被标记的对象.这个算法两个问题.一个是效率问题,标记和 ...

  8. bootstrap之css样式

    一 bootstrap的介绍 Bootstrap是将html,css和js的代码打包好了,只管我们拿来调用.是基于jquery开发的. 使用BootCDN提供的免费CDN加速服务,同时支持http和h ...

  9. 2019.01.24 bzoj2310: ParkII(轮廓线dp)

    传送门 题意简述:给一个m*n的矩阵,每个格子有权值V(i,j) (可能为负数),要求找一条路径,使得每个点最多经过一次且点权值之和最大. 思路:我们将求回路时的状态定义改进一下. 现在由于求的是路径 ...

  10. 乌龙之Ignoring query to other database问题

    问题现象: [root@zxdb05 ~]# mysql -root -pEnter password: Welcome to the MySQL monitor.  Commands end wit ...