微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ItemsControl 的布局控件实例

示例

1、ItemsstackPanel 的示例
Controls/CollectionControl/ItemsControlDemo/LayoutControl/ItemsstackPanelDemo.xaml

<Pagex:Class=Windows10.Controls.CollectionControl.ItemsControlDemo.LayoutControl.ItemsstackPanelDemoxmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:x=http://schemas.microsoft.com/winfx/2006/xamlxmlns:local=using:Windows10.Controls.CollectionControl.ItemsControlDemo.LayoutControlxmlns:d=http://schemas.microsoft.com/expression/blend/2008xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006mc: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.ItemsWrapGridDemoxmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:x=http://schemas.microsoft.com/winfx/2006/xamlxmlns:local=using:Windows10.Controls.CollectionControl.ItemsControlDemo.LayoutControlxmlns:d=http://schemas.microsoft.com/expression/blend/2008xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006mc: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
[源码下载]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐