背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组
作者:webabcd
介绍
背水一战 Windows 10 之 控件(集合类 - ItemsControl)
- 项模板选择器
- 数据分组
示例
1、ItemsControl 的项模板选择器
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo3.xaml
- <Page
- x:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.ItemsControlDemo3"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:Windows10.Controls.CollectionControl.ItemsControlDemo"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d"
- xmlns:common="using:Windows10.Common">
- <Page.Resources>
- <!--
- DataTemplate - 数据模板
- -->
- <DataTemplate x:DataType="common:Employee" x:Key="DataTemplateMale">
- <Grid Background="Blue">
- <TextBlock Text="{x:Bind Name}" />
- </Grid>
- </DataTemplate>
- <DataTemplate x:DataType="common:Employee" x:Key="DataTemplateFemale">
- <Grid Background="Pink">
- <TextBlock Text="{x:Bind Name}" />
- </Grid>
- </DataTemplate>
- <!--
- 自定义数据模板选择器(参见 code-behind 中的代码)
- -->
- <local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
- DataTemplate1="{StaticResource DataTemplateMale}"
- DataTemplate2="{StaticResource DataTemplateFemale}" />
- </Page.Resources>
- <Grid Background="Transparent">
- <StackPanel Margin="10 0 10 10" Orientation="Horizontal">
- <!--
- ItemsControl - 集合控件
- ItemTemplateSelector - 每个数据项的数据模板选择器(如果指定了 ItemTemplate 则此配置无效)
- -->
- <ListView Name="itemsControl" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top"
- ItemsSource="{x:Bind Employees}"
- ItemTemplateSelector="{StaticResource MyDataTemplateSelector}">
- </ListView>
- </StackPanel>
- </Grid>
- </Page>
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo3.xaml.cs
- /*
- * ItemsControl - 集合控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
- *
- *
- * 本例用于演示 ItemsControl 如何通过 item 的不同而使用不同的数据模板
- */
- using System.Collections.ObjectModel;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows10.Common;
- namespace Windows10.Controls.CollectionControl.ItemsControlDemo
- {
- public sealed partial class ItemsControlDemo3 : Page
- {
- public ObservableCollection<Employee> Employees { get; set; } = TestData.GetEmployees();
- public ItemsControlDemo3()
- {
- this.InitializeComponent();
- }
- }
- // 自定义 DataTemplateSelector(数据模板选择器)
- // 可以实现在 runtime 时,根据 item 的不同选择不同的数据模板
- public class MyDataTemplateSelector : DataTemplateSelector
- {
- // 数据模板 1(配置在 xaml 端)
- public DataTemplate DataTemplate1 { get; set; }
- // 数据模板 2(配置在 xaml 端)
- public DataTemplate DataTemplate2 { get; set; }
- // 根据 item 的数据的不同,指定的不同的模板
- protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
- {
- var employee = item as Employee;
- if (employee == null || employee.IsMale)
- return DataTemplate1; // 男员工用数据模板 1
- return DataTemplate2; // 女员工用数据模板 2
- // 如果想直接返回指定的资源也是可以的(但是不灵活),类似:return (DataTemplate)Application.Current.Resources["DataTemplateMale"];
- }
- }
- }
2、ItemsControl 的数据分组
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo4.xaml
- <Page
- x:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.ItemsControlDemo4"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local="using:Windows10.Controls.CollectionControl.ItemsControlDemo"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d">
- <Page.Resources>
- <!--
- GroupStyle - 组样式
- HidesIfEmpty - 空组是否隐藏
- HeaderContainerStyle - 组标题的容器样式
- HeaderTemplate - 组标题的模板
- HeaderTemplateSelector - 组标题的模板选择器
- 注:
- ListView 的 Group 的 HeaderContainer 是 ListViewHeaderItem, GridView 的 Group 的 HeaderContainer 是 GridViewHeaderItem
- ListViewHeaderItem 和 GridViewHeaderItem 均继承自 ListViewBaseHeaderItem, ListViewBaseHeaderItem 继承自 ContentControl
- -->
- <GroupStyle x:Key="GroupStyle1" HeaderTemplate="{StaticResource DataTemplateGroupHeader}">
- <GroupStyle.HeaderContainerStyle>
- <Style TargetType="ListViewHeaderItem">
- <Setter Property="Background" Value="Blue" />
- </Style>
- </GroupStyle.HeaderContainerStyle>
- </GroupStyle>
- <GroupStyle x:Key="GroupStyle2" HeaderTemplate="{StaticResource DataTemplateGroupHeader}">
- <GroupStyle.HeaderContainerStyle>
- <Style TargetType="ListViewHeaderItem">
- <Setter Property="Background" Value="Orange" />
- </Style>
- </GroupStyle.HeaderContainerStyle>
- </GroupStyle>
- <DataTemplate x:Key="DataTemplateGroupHeader">
- <TextBlock Text="{Binding Title}" />
- </DataTemplate>
- <!--
- 自定义 GroupStyle 选择器(参见 code-behind 中的代码)
- -->
- <local:MyGroupStyleSelector x:Key="MyGroupStyleSelector"
- GroupStyle1="{StaticResource GroupStyle1}"
- GroupStyle2="{StaticResource GroupStyle2}" />
- </Page.Resources>
- <Grid Background="Transparent">
- <StackPanel Margin="10 0 10 10">
- <!--
- ItemsControl - 集合控件
- ItemsPanel - 用于指定 items 的布局控件,任何 Panel 类型的布局控件均可,所有 items 将在 Panel 内显示(Panel 是所有 items 的容器)
- 给 ItemsControl 用的,可虚拟化的布局控件有:ItemsStackPanel, ItemsWrapGrid, VirtualizingStackPanel, WrapGrid. 请参见:/Controls/CollectionControl/ItemsControlDemo/LayoutControl/
- GroupStyle - 组样式
- GroupStyleSelector - 组样式选择器
- -->
- <ListView Name="listView" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" ItemsSource="{x:Bind MyData.View}"
- GroupStyleSelector="{StaticResource MyGroupStyleSelector}" SelectionChanged="listView_SelectionChanged">
- <!--
- <ListView.GroupStyle>
- <GroupStyle>
- <GroupStyle.HeaderTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Title}" />
- </DataTemplate>
- </GroupStyle.HeaderTemplate>
- </GroupStyle>
- </ListView.GroupStyle>
- -->
- <ListView.ItemTemplate>
- <DataTemplate>
- <TextBlock Text="{Binding Title}" Foreground="Purple" />
- </DataTemplate>
- </ListView.ItemTemplate>
- <ListView.ItemsPanel>
- <ItemsPanelTemplate>
- <ItemsStackPanel />
- </ItemsPanelTemplate>
- </ListView.ItemsPanel>
- </ListView>
- <TextBlock Name="lblMsg" Margin="5" />
- </StackPanel>
- </Grid>
- </Page>
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo4.xaml.cs
- /*
- * ItemsControl - 集合控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/)
- * IsGrouping - 当前 ItemsControl 显示的是否是分组数据(只读)
- * DependencyObject GroupHeaderContainerFromItemContainer(DependencyObject itemContainer) - 获取指定 ItemContainer 的 HeaderContainer
- *
- *
- * 本例用于演示如何通过 ItemsControl 显示分组数据
- *
- * 注:本例是用 ListView 来演示数据分组的,用 GridView 做数据分组的示例请参见 /Index.xaml
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Xml.Linq;
- using Windows.UI.Popups;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.UI.Xaml.Data;
- using Windows10.Common;
- namespace Windows10.Controls.CollectionControl.ItemsControlDemo
- {
- public sealed partial class ItemsControlDemo4 : Page
- {
- public CollectionViewSource MyData
- {
- get
- {
- XElement root = XElement.Load("SiteMap.xml");
- var items = LoadData(root);
- // 构造数据源
- CollectionViewSource source = new CollectionViewSource();
- source.IsSourceGrouped = true;
- source.Source = items;
- source.ItemsPath = new PropertyPath("Items");
- return source;
- }
- }
- public ItemsControlDemo4()
- {
- this.InitializeComponent();
- this.Loaded += ItemsControlDemo4_Loaded;
- }
- private void ItemsControlDemo4_Loaded(object sender, RoutedEventArgs e)
- {
- lblMsg.Text = "IsGrouping: " + listView.IsGrouping.ToString();
- }
- private async void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- if (e.AddedItems.Count > && listView.ContainerFromItem(e.AddedItems[]) != null)
- {
- // 获取选中数据的 HeaderContainer
- ListViewHeaderItem headerContainer = listView.GroupHeaderContainerFromItemContainer(listView.ContainerFromItem(e.AddedItems[])) as ListViewHeaderItem;
- NavigationModel headerNavigationModel = headerContainer.Content as NavigationModel;
- await new MessageDialog($"header: {headerNavigationModel.Title}").ShowAsync();
- }
- }
- // 解析 xml 数据
- private List<NavigationModel> LoadData(XElement root)
- {
- if (root == null)
- return null;
- var items = from n in root.Elements("node")
- select new NavigationModel
- {
- Title = (string)n.Attribute("title"),
- Url = (string)n.Attribute("url"),
- Items = LoadData(n)
- };
- return items.ToList();
- }
- }
- // 自定义 MyGroupStyleSelector(GroupStyle 选择器)
- // 可以实现在 runtime 时,根据 group 的不同选择不同的 GroupStyle
- public class MyGroupStyleSelector : GroupStyleSelector
- {
- static bool temp = false;
- // GroupStyle 1(配置在 xaml 端)
- public GroupStyle GroupStyle1 { get; set; }
- // GroupStyle 2(配置在 xaml 端)
- public GroupStyle GroupStyle2 { get; set; }
- protected override GroupStyle SelectGroupStyleCore(object group, uint level)
- {
- // 我这里测试,group 要么是 null 要么是 DependencyObject,level 总是 0
- // 利用这个变量,来演示如何让不同的 group 使用不同的 GroupStyle
- temp ^= true;
- if (temp)
- return GroupStyle1;
- return GroupStyle2;
- // 如果想直接返回指定的资源也是可以的(但是不灵活),类似:return (GroupStyle)Application.Current.Resources["GroupStyle1"];
- }
- }
- }
OK
[源码下载]
背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组的更多相关文章
- 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresenter, ListViewItemPresenter
[源码下载] 背水一战 Windows 10 (50) - 控件(集合类): ItemsControl - 基础知识, 数据绑定, ItemsPresenter, GridViewItemPresen ...
- 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter
[源码下载] 背水一战 Windows 10 (52) - 控件(集合类): ItemsControl - 自定义 ItemsControl, 自定义 ContentPresenter 作者:weba ...
- 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid
[源码下载] 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid 作者:webabc ...
- 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingStackPanel, WrapGrid
[源码下载] 背水一战 Windows 10 (54) - 控件(集合类): ItemsControl 的布局控件 - OrientedVirtualizingPanel, VirtualizingS ...
- 背水一战 Windows 10 (49) - 控件(集合类): Pivot, Hub
[源码下载] 背水一战 Windows 10 (49) - 控件(集合类): Pivot, Hub 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) Pivot Hub 示 ...
- 背水一战 Windows 10 (48) - 控件(集合类): FlipView
[源码下载] 背水一战 Windows 10 (48) - 控件(集合类): FlipView 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合类) FlipView 示例Fl ...
- 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation
[源码下载] 背水一战 Windows 10 (55) - 控件(集合类): SemanticZoom, ISemanticZoomInformation 作者:webabcd 介绍背水一战 Wind ...
- 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项
[源码下载] 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合 ...
- 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制
[源码下载] 背水一战 Windows 10 (57) - 控件(集合类): ListViewBase - 增量加载, 分步绘制 作者:webabcd 介绍背水一战 Windows 10 之 控件(集 ...
随机推荐
- mybatis 复杂传参
1基本传参数 Public User selectUserWithCon(@param(“userName”)String name,@param(“userArea”)String area); ...
- idea配置springBoot项目热加载
1.在application.properties中禁用模板引擎缓存 比如freemarker:spring.freemarker.cache=false 2.在pom.xml中添加依赖 <de ...
- 【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. ...
- C语言基础第四次作业
题目7-2,九九乘法表 1.实验代码: #include<stdio.h> int main() { int N, i, j, q; scanf("%d",&N ...
- linux_开启mysql服务
想要连接mysql的时候必须先开启mysql的服务 service mysqld start mysql -u root -p 输入密码
- 使用delphi 10.2 开发linux 上的Daemon
delphi 10.2 支持linux, 而且官方只是支持命令行编程,目地就是做linux 服务器端的开发. 既然是做linux服务器端的开发,那么普通的命令行运行程序,然后等待开一个黑窗口的方式就 ...
- java笔记--问题总结
1. 垃圾回收算法 标记-清除算法 标记-清除算法是最基本的算法,和他的名字一样,分为两个步骤,一个步骤是标记需要回收的对象.在标记完成后统一回收被标记的对象.这个算法两个问题.一个是效率问题,标记和 ...
- bootstrap之css样式
一 bootstrap的介绍 Bootstrap是将html,css和js的代码打包好了,只管我们拿来调用.是基于jquery开发的. 使用BootCDN提供的免费CDN加速服务,同时支持http和h ...
- 2019.01.24 bzoj2310: ParkII(轮廓线dp)
传送门 题意简述:给一个m*n的矩阵,每个格子有权值V(i,j) (可能为负数),要求找一条路径,使得每个点最多经过一次且点权值之和最大. 思路:我们将求回路时的状态定义改进一下. 现在由于求的是路径 ...
- 乌龙之Ignoring query to other database问题
问题现象: [root@zxdb05 ~]# mysql -root -pEnter password: Welcome to the MySQL monitor. Commands end wit ...