[源码下载]

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

作者:webabcd

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

  • ItemsStackPanel
  • ItemsWrapGrid

示例
1、ItemsStackPanel 的示例
Controls/CollectionControl/ItemsControlDemo/LayoutControl/ItemsStackPanelDemo.xaml

<Page
x:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.LayoutControl.ItemsStackPanelDemo"
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.LayoutControl"
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"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10" Orientation="Horizontal"> <StackPanel Margin="5">
<!--
ItemsStackPanel - 虚拟化布局控件,ListView 的默认布局控件
Orientation - 子元素的排列方向
Vertical - 垂直排列,默认值
Horizontal - 水平排列
CacheLength - 可见区外的需要缓存的数据的大小(以可见区条数大小的倍数为单位),默认值为 4.0
比如当可见区可以显示 10 条数据,CacheLength 为 4 时,可见区外的需要缓存的数据的大小则为 4 * 10 = 40,也就是说整个缓存数据的大小为 10 + 4 * 10 = 50
实际测试发现,可能会有一定的偏差,但是大体是准确的
-->
<ListView Name="listView1" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" ItemsSource="{x:Bind MyData.View}">
<ListView.ItemTemplate>
<DataTemplate x:DataType="common:NavigationModel">
<Grid Background="Blue">
<TextBlock Text="{x:Bind Title}" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel Orientation="Vertical" CacheLength="4" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<TextBlock Name="lblMsg1" Margin="5" />
</StackPanel> <StackPanel Margin="5">
<!--
ItemsStackPanel - 虚拟化布局控件,ListView 的默认布局控件
GroupPadding - 每一个数据组的 padding
GroupHeaderPlacement - 每一个数据组的 header 的显示位置
Top - 顶部。默认值
Left - 左侧
AreStickyGroupHeadersEnabled - 组 header 是否是固定的,即不随组数据的滚动而滚动。默认值为 true
-->
<ListView Name="listView2" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" ItemsSource="{x:Bind MyData.View}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsStackPanel GroupPadding="4"
GroupHeaderPlacement="Top"
AreStickyGroupHeadersEnabled="{Binding IsChecked, ElementName=chkAreStickyGroupHeadersEnabled}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<ComboBox x:Name="cmbGroupHeaderPlacement" Margin="5" PlaceholderText="GroupHeaderPlacement" SelectionChanged="cmbGroupHeaderPlacement_SelectionChanged">
<ComboBoxItem>Top</ComboBoxItem>
<ComboBoxItem>Left</ComboBoxItem>
</ComboBox>
<CheckBox Name="chkAreStickyGroupHeadersEnabled" Content="AreStickyGroupHeadersEnabled" IsChecked="True" Margin="5" />
</StackPanel> </StackPanel>
</Grid>
</Page>

Controls/CollectionControl/ItemsControlDemo/LayoutControl/ItemsStackPanelDemo.xaml.cs

/*
* ItemsStackPanel - 虚拟化布局控件,ListView 的默认布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
* FirstCacheIndex - 缓存中的第一项在全部数据中的索引位置
* FirstVisibleIndex - 屏幕上显示的第一项在全部数据中的索引位置
* LastCacheIndex - 缓存中的最后一项在全部数据中的索引位置
* LastVisibleIndex - 屏幕上显示的最后一项在全部数据中的索引位置
* CacheLength - 可见区外的需要缓存的数据的大小(以可见区条数大小的倍数为单位),默认值为 4.0
* 比如当可见区可以显示 10 条数据,CacheLength 为 4 时,可见区外的需要缓存的数据的大小则为 4 * 10 = 40,也就是说整个缓存数据的大小为 10 + 4 * 10 = 50
* 实际测试发现,可能会有一定的偏差,但是大体是准确的
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows10.Common; namespace Windows10.Controls.CollectionControl.ItemsControlDemo.LayoutControl
{
public sealed partial class ItemsStackPanelDemo : 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;
}
} private ItemsStackPanel _itemsStackPanel1 = null;
private ItemsStackPanel _itemsStackPanel2 = null; public ItemsStackPanelDemo()
{
this.InitializeComponent(); this.Loaded += ItemsStackPanelDemo_Loaded;
} private void ItemsStackPanelDemo_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer dTimer = new DispatcherTimer();
dTimer.Interval = TimeSpan.Zero;
dTimer.Tick += DTimer_Tick;
dTimer.Start(); // 获取 ListView 中的 ItemsStackPanel 控件
_itemsStackPanel1 = listView1.ItemsPanelRoot as ItemsStackPanel;
_itemsStackPanel2 = listView2.ItemsPanelRoot as ItemsStackPanel; // 获取 ListView 中的 ItemsStackPanel 控件
// _itemsStackPanel1 = Helper.GetVisualChild<ItemsStackPanel>(listView1);
// _itemsStackPanel2 = Helper.GetVisualChild<ItemsStackPanel>(listView2);
} private void DTimer_Tick(object sender, object e)
{
lblMsg1.Text = "FirstCacheIndex: " + _itemsStackPanel1.FirstCacheIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "FirstVisibleIndex: " + _itemsStackPanel1.FirstVisibleIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "LastCacheIndex: " + _itemsStackPanel1.LastCacheIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "LastVisibleIndex: " + _itemsStackPanel1.LastVisibleIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "CacheLength: " + _itemsStackPanel1.CacheLength.ToString();
} private void cmbGroupHeaderPlacement_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_itemsStackPanel2.GroupHeaderPlacement = (GroupHeaderPlacement)Enum.Parse(typeof(GroupHeaderPlacement), (e.AddedItems[] as ComboBoxItem).Content.ToString());
} // 解析 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();
}
}
}

2、ItemsWrapGrid 的示例
Controls/CollectionControl/ItemsControlDemo/LayoutControl/ItemsWrapGridDemo.xaml

<Page
x:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.LayoutControl.ItemsWrapGridDemo"
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.LayoutControl"
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"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10" Orientation="Horizontal"> <StackPanel Margin="5">
<!--
ItemsWrapGrid - 虚拟化布局控件,GridView 的默认布局控件
Orientation - 子元素的排列方向
Vertical - 垂直排列,默认值
Horizontal - 水平排列
ItemWidth - 每个 item 的宽
ItemHeight - 每个 item 的高
MaximumRowsOrColumns - 最大行数或最大列数(默认值为 -1)
CacheLength - 可见区外的需要缓存的数据的大小(以可见区条数大小的倍数为单位),默认值为 4.0
比如当可见区可以显示 10 条数据,CacheLength 为 4 时,可见区外的需要缓存的数据的大小则为 4 * 10 = 40,也就是说整个缓存数据的大小为 10 + 4 * 10 = 50
实际测试发现,可能会有一定的偏差,但是大体是准确的
-->
<GridView Name="gridView1" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" ItemsSource="{x:Bind MyData.View}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="common:NavigationModel">
<Grid Background="Blue">
<TextBlock Text="{x:Bind Title}" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<GridView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" ItemWidth="120" ItemHeight="50" MaximumRowsOrColumns="3" CacheLength="4" />
</ItemsPanelTemplate>
</GridView.ItemsPanel>
</GridView>
<TextBlock Name="lblMsg1" Margin="5" />
</StackPanel> <StackPanel Margin="5">
<!--
ItemsWrapGrid - 虚拟化布局控件,GridView 的默认布局控件
GroupPadding - 每一个数据组的 padding
GroupHeaderPlacement - 每一个数据组的 header 的显示位置
Top - 顶部。默认值
Left - 左侧
AreStickyGroupHeadersEnabled - 组 header 是否是固定的,即不随组数据的滚动而滚动。默认值为 true
-->
<ListView Name="gridView2" Margin="5" Width="400" Height="400" HorizontalAlignment="Left" ItemsSource="{x:Bind MyData.View}">
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" />
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListView.GroupStyle>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" Width="100" />
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<ItemsWrapGrid Orientation="Horizontal" MaximumRowsOrColumns="3"
GroupPadding="4"
GroupHeaderPlacement="Top"
AreStickyGroupHeadersEnabled="{Binding IsChecked, ElementName=chkAreStickyGroupHeadersEnabled}" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
<ComboBox x:Name="cmbGroupHeaderPlacement" Margin="5" PlaceholderText="GroupHeaderPlacement" SelectionChanged="cmbGroupHeaderPlacement_SelectionChanged">
<ComboBoxItem>Top</ComboBoxItem>
<ComboBoxItem>Left</ComboBoxItem>
</ComboBox>
<CheckBox Name="chkAreStickyGroupHeadersEnabled" Content="AreStickyGroupHeadersEnabled" IsChecked="True" Margin="5" />
</StackPanel> </StackPanel>
</Grid>
</Page>

Controls/CollectionControl/ItemsControlDemo/LayoutControl/ItemsWrapGridDemo.xaml.cs

/*
* ItemsWrapGrid - 虚拟化布局控件,GridView 的默认布局控件(继承自 Panel, 请参见 /Controls/LayoutControl/PanelDemo.xaml)
* FirstCacheIndex - 缓存中的第一项在全部数据中的索引位置
* FirstVisibleIndex - 屏幕上显示的第一项在全部数据中的索引位置
* LastCacheIndex - 缓存中的最后一项在全部数据中的索引位置
* LastVisibleIndex - 屏幕上显示的最后一项在全部数据中的索引位置
* CacheLength - 可见区外的需要缓存的数据的大小(以可见区条数大小的倍数为单位),默认值为 4.0
* 比如当可见区可以显示 10 条数据,CacheLength 为 4 时,可见区外的需要缓存的数据的大小则为 4 * 10 = 40,也就是说整个缓存数据的大小为 10 + 4 * 10 = 50
* 实际测试发现,可能会有一定的偏差,但是大体是准确的
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows10.Common; namespace Windows10.Controls.CollectionControl.ItemsControlDemo.LayoutControl
{
public sealed partial class ItemsWrapGridDemo : 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;
}
} private ItemsWrapGrid _itemsWrapGrid1 = null;
private ItemsWrapGrid _itemsWrapGrid2 = null; public ItemsWrapGridDemo()
{
this.InitializeComponent(); this.Loaded += ItemsWrapGridDemo_Loaded;
} private void ItemsWrapGridDemo_Loaded(object sender, RoutedEventArgs e)
{
DispatcherTimer dTimer = new DispatcherTimer();
dTimer.Interval = TimeSpan.Zero;
dTimer.Tick += DTimer_Tick;
dTimer.Start(); // 获取 GridView 中的 ItemsWrapGrid 控件
_itemsWrapGrid1 = gridView1.ItemsPanelRoot as ItemsWrapGrid;
_itemsWrapGrid2 = gridView2.ItemsPanelRoot as ItemsWrapGrid; // 获取 GridView 中的 ItemsWrapGrid 控件
// _itemsWrapGrid1 = Helper.GetVisualChild<ItemsWrapGrid>(gridView1);
// _itemsWrapGrid2 = Helper.GetVisualChild<ItemsWrapGrid>(gridView2);
} private void DTimer_Tick(object sender, object e)
{
lblMsg1.Text = "FirstCacheIndex: " + _itemsWrapGrid1.FirstCacheIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "FirstVisibleIndex: " + _itemsWrapGrid1.FirstVisibleIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "LastCacheIndex: " + _itemsWrapGrid1.LastCacheIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "LastVisibleIndex: " + _itemsWrapGrid1.LastVisibleIndex.ToString();
lblMsg1.Text += Environment.NewLine;
lblMsg1.Text += "CacheLength: " + _itemsWrapGrid1.CacheLength.ToString();
} private void cmbGroupHeaderPlacement_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_itemsWrapGrid2.GroupHeaderPlacement = (GroupHeaderPlacement)Enum.Parse(typeof(GroupHeaderPlacement), (e.AddedItems[] as ComboBoxItem).Content.ToString());
} // 解析 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();
}
}
}

OK
[源码下载]

背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid的更多相关文章

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

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

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

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

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

    [源码下载] 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组 作者:webabcd 介绍背水一战 Windows 10 之 控件 ...

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

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

  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. 解决mysql安装出现error Nr.1045问题

    我们在windows下安装mysql最后一步时会出现Access denied for user 'root'@localhost'(using password:No)的问题.这几个问题经常出现在卸 ...

  2. Ubuntu上搭建Hadoop环境(单机模式+伪分布模式) (转载)

    Hadoop在处理海量数据分析方面具有独天优势.今天花了在自己的Linux上搭建了伪分布模式,期间经历很多曲折,现在将经验总结如下. 首先,了解Hadoop的三种安装模式: 1. 单机模式. 单机模式 ...

  3. 使用 CXF 做 webservice 简单例子(转载)

    使用 CXF 做 webservice 简单例子     Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这 ...

  4. MathExam

    MathExam 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 575 605 • Est ...

  5. 导入mysql报错问题

    今天数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max ...

  6. kbmmw 中JSON 中使用SQL 查询

    前面讲到了kbmmw 的JSON 对象操作,如何快速的查找JSON 中的值? 一种办法就是通过遍历的方法,其实在kbmmw 还有一种灵活的查询方式, 就是通过SQL 方式查询JSON 中的值.也就是说 ...

  7. MySQL按日、周、月统计数据

    知识关键词:DATE_FORMAT ps:如果时间字段为时间戳则,DATE_FORMAT(from_unixtime(create_time),'%Y-%u') select DATE_FORMAT( ...

  8. ACM-ICPC 2018 徐州赛区网络预赛 J Maze Designer(最大生成树,倍增lca)

    https://nanti.jisuanke.com/t/31462 要求在一个矩形中任意选两个点都有唯一的通路,所以不会建多余的墙. 要求满足上述情况下,建墙的费用最小.理解题意后容易想到首先假设全 ...

  9. 关于Excel分析图插入到论文的问题

    为了保证插入到latex图片不失真,可将Excel中的图进行如下操作: 1.将Excel分析图另存为.pdf格式: 2.利用Adobe acrobat裁剪掉空白的部分,另存为.eps格式: 3.将ep ...

  10. 删除一直处于deleting状态的数据卷

    一.场景 有一个volume数据卷hzb-1G-xxxxxx创建在nc8的ceph节点上,并且该数据卷的但是有一天nc8节点坏掉了.当我们删除hzb-1G-xxxxxx的时候,就会一直处于deleti ...