背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组
2021-05-12 19:26
标签:style sys sso web 布局 tar logs xmlns 测试 [源码下载] 作者:webabcd 示例 Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo3.xaml.cs Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo4.xaml.cs OK 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组 标签:style sys sso web 布局 tar logs xmlns 测试 原文地址:http://www.cnblogs.com/lonelyxmas/p/7568261.html
介绍
背水一战 Windows 10 之 控件(集合类 - ItemsControl)
1、ItemsControl 的项模板选择器
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo3.xamlPage
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 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>
local:MyDataTemplateSelector x:Key="MyDataTemplateSelector"
DataTemplate1="{StaticResource DataTemplateMale}"
DataTemplate2="{StaticResource DataTemplateFemale}" />
Page.Resources>
Grid Background="Transparent">
StackPanel Margin="10 0 10 10" Orientation="Horizontal">
ListView Name="itemsControl" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" VerticalAlignment="Top"
ItemsSource="{x:Bind Employees}"
ItemTemplateSelector="{StaticResource MyDataTemplateSelector}">
ListView>
StackPanel>
Grid>
Page>
/*
* 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
2、ItemsControl 的数据分组
Controls/CollectionControl/ItemsControlDemo/ItemsControlDemo4.xamlPage
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 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>
local:MyGroupStyleSelector x:Key="MyGroupStyleSelector"
GroupStyle1="{StaticResource GroupStyle1}"
GroupStyle2="{StaticResource GroupStyle2}" />
Page.Resources>
Grid Background="Transparent">
StackPanel Margin="10 0 10 10">
ListView Name="listView" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" ItemsSource="{x:Bind MyData.View}"
GroupStyleSelector="{StaticResource MyGroupStyleSelector}" SelectionChanged="listView_SelectionChanged">
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>
/*
* 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 > 0 && listView.ContainerFromItem(e.AddedItems[0]) != null)
{
// 获取选中数据的 HeaderContainer
ListViewHeaderItem headerContainer = listView.GroupHeaderContainerFromItemContainer(listView.ContainerFromItem(e.AddedItems[0])) as ListViewHeaderItem;
NavigationModel headerNavigationModel = headerContainer.Content as NavigationModel;
await new MessageDialog($"header: {headerNavigationModel.Title}").ShowAsync();
}
}
// 解析 xml 数据
private List
[源码下载]
文章标题:背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组
文章链接:http://soscw.com/index.php/essay/84815.html