首页 后端开发 C#.Net教程 ItemsControl 的布局控件实例

ItemsControl 的布局控件实例

Jun 23, 2017 pm 03:16 PM
windows 控件 集合

示例

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

<Pagex: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[0] 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

<Pagex: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[0] 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
[源码下载]

以上是ItemsControl 的布局控件实例的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1664
14
CakePHP 教程
1423
52
Laravel 教程
1319
25
PHP教程
1269
29
C# 教程
1248
24
mysql怎么复制粘贴 mysql怎么复制粘贴 Apr 08, 2025 pm 07:18 PM

MySQL 中的复制粘贴包含以下步骤:选择数据,使用 Ctrl C(Windows)或 Cmd C(Mac)复制;在目标位置右键单击,选择“粘贴”或使用 Ctrl V(Windows)或 Cmd V(Mac);复制的数据将插入到目标位置,或替换现有数据(取决于目标位置是否已存在数据)。

vs code 可以在 Windows 8 中运行吗 vs code 可以在 Windows 8 中运行吗 Apr 15, 2025 pm 07:24 PM

VS Code可以在Windows 8上运行,但体验可能不佳。首先确保系统已更新到最新补丁,然后下载与系统架构匹配的VS Code安装包,按照提示安装。安装后,注意某些扩展程序可能与Windows 8不兼容,需要寻找替代扩展或在虚拟机中使用更新的Windows系统。安装必要的扩展,检查是否正常工作。尽管VS Code在Windows 8上可行,但建议升级到更新的Windows系统以获得更好的开发体验和安全保障。

sublime写好代码后如何运行 sublime写好代码后如何运行 Apr 16, 2025 am 08:51 AM

在 Sublime 中运行代码的方法有六种:通过热键、菜单、构建系统、命令行、设置默认构建系统和自定义构建命令,并可通过右键单击项目/文件运行单个文件/项目,构建系统可用性取决于 Sublime Text 的安装情况。

Debian中Tigervnc支持哪些操作系统 Debian中Tigervnc支持哪些操作系统 Apr 12, 2025 pm 10:15 PM

开源VNC工具Tigervnc兼容众多操作系统,其中包括Windows、Linux和macOS。本文将详细介绍Tigervnc在Debian系统上的应用情况。Tigervnc在Debian系统的应用系统集成:在Debian系统中,Tigervnc作为VNC服务器组件被集成到系统中。用户可通过命令行工具(例如vncserver)启动VNC服务,并自定义显示设置,如分辨率和色彩深度。跨平台连接:Tigervnc客户端支持Windows、Linux和macOS,这意味着用户可以从任何运行这

如何解决Laravel中复杂的BelongsToThrough关系问题?使用Composer可以! 如何解决Laravel中复杂的BelongsToThrough关系问题?使用Composer可以! Apr 17, 2025 pm 09:54 PM

在Laravel开发中,处理复杂的模型关系一直是个挑战,特别是当涉及到多层级的BelongsToThrough关系时。最近,我在处理一个多级模型关系的项目中遇到了这个问题,传统的HasManyThrough关系无法满足需求,导致数据查询变得复杂且低效。经过一番探索,我找到了staudenmeir/belongs-to-through这个库,它通过Composer轻松安装并解决了我的困扰。

laravel安装代码 laravel安装代码 Apr 18, 2025 pm 12:30 PM

要安装 Laravel,需依序进行以下步骤:安装 Composer(适用于 macOS/Linux 和 Windows)安装 Laravel 安装器创建新项目启动服务访问应用程序(网址:http://127.0.0.1:8000)设置数据库连接(如果需要)

Mac系统中的系统维护与优化工具推荐 Mac系统中的系统维护与优化工具推荐 Apr 12, 2025 pm 04:45 PM

Mac 系统维护包括:磁盘管理(使用 OmniDiskSweeper 清理磁盘空间,用磁盘工具检查磁盘错误)内存管理(用 Activity Monitor 监控内存占用,结束占用过高的进程)启动项管理(用 Linc 或 LaunchControl 管理启动项,禁用不必要的启动项)系统缓存清理(用 CleanMyMac X 或手动清理系统缓存)软件更新(及时更新系统和应用程序)定期备份(使用 Time Machine 定期备份数据)良好使用习惯(不过度安装应用程序,定期清理文件,监控系统日志)

git软件安装 git软件安装 Apr 17, 2025 am 11:57 AM

安装 Git 软件包括以下步骤:下载安装包运行安装包验证安装配置 Git安装 Git Bash(仅限 Windows)

See all articles