這篇文章主要介紹了ItemsControl布局控件怎么用,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)建站公司2013年成立,先為鶴城等服務(wù)建站,鶴城等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為鶴城企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
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 的默認(rèn)布局控件
Orientation - 子元素的排列方向
Vertical - 垂直排列,默認(rèn)值
Horizontal - 水平排列
CacheLength - 可見區(qū)外的需要緩存的數(shù)據(jù)的大小(以可見區(qū)條數(shù)大小的倍數(shù)為單位),默認(rèn)值為 4.0
比如當(dāng)可見區(qū)可以顯示 10 條數(shù)據(jù),CacheLength 為 4 時(shí),可見區(qū)外的需要緩存的數(shù)據(jù)的大小則為 4 * 10 = 40,也就是說整個(gè)緩存數(shù)據(jù)的大小為 10 + 4 * 10 = 50
實(shí)際測試發(fā)現(xiàn),可能會(huì)有一定的偏差,但是大體是準(zhǔn)確的--><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 的默認(rèn)布局控件
GroupPadding - 每一個(gè)數(shù)據(jù)組的 padding
GroupHeaderPlacement - 每一個(gè)數(shù)據(jù)組的 header 的顯示位置
Top - 頂部。默認(rèn)值
Left - 左側(cè)
AreStickyGroupHeadersEnabled - 組 header 是否是固定的,即不隨組數(shù)據(jù)的滾動(dòng)而滾動(dòng)。默認(rèn)值為 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 的默認(rèn)布局控件(繼承自 Panel, 請(qǐng)參見 /Controls/LayoutControl/PanelDemo.xaml)
* FirstCacheIndex - 緩存中的第一項(xiàng)在全部數(shù)據(jù)中的索引位置
* FirstVisibleIndex - 屏幕上顯示的第一項(xiàng)在全部數(shù)據(jù)中的索引位置
* LastCacheIndex - 緩存中的最后一項(xiàng)在全部數(shù)據(jù)中的索引位置
* LastVisibleIndex - 屏幕上顯示的最后一項(xiàng)在全部數(shù)據(jù)中的索引位置
* CacheLength - 可見區(qū)外的需要緩存的數(shù)據(jù)的大小(以可見區(qū)條數(shù)大小的倍數(shù)為單位),默認(rèn)值為 4.0
* 比如當(dāng)可見區(qū)可以顯示 10 條數(shù)據(jù),CacheLength 為 4 時(shí),可見區(qū)外的需要緩存的數(shù)據(jù)的大小則為 4 * 10 = 40,也就是說整個(gè)緩存數(shù)據(jù)的大小為 10 + 4 * 10 = 50
* 實(shí)際測試發(fā)現(xiàn),可能會(huì)有一定的偏差,但是大體是準(zhǔn)確的 */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);// 構(gòu)造數(shù)據(jù)源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 數(shù)據(jù)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 的默認(rèn)布局控件
Orientation - 子元素的排列方向
Vertical - 垂直排列,默認(rèn)值
Horizontal - 水平排列
ItemWidth - 每個(gè) item 的寬
ItemHeight - 每個(gè) item 的高
MaximumRowsOrColumns - 最大行數(shù)或最大列數(shù)(默認(rèn)值為 -1)
CacheLength - 可見區(qū)外的需要緩存的數(shù)據(jù)的大小(以可見區(qū)條數(shù)大小的倍數(shù)為單位),默認(rèn)值為 4.0
比如當(dāng)可見區(qū)可以顯示 10 條數(shù)據(jù),CacheLength 為 4 時(shí),可見區(qū)外的需要緩存的數(shù)據(jù)的大小則為 4 * 10 = 40,也就是說整個(gè)緩存數(shù)據(jù)的大小為 10 + 4 * 10 = 50
實(shí)際測試發(fā)現(xiàn),可能會(huì)有一定的偏差,但是大體是準(zhǔn)確的--><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 的默認(rèn)布局控件
GroupPadding - 每一個(gè)數(shù)據(jù)組的 padding
GroupHeaderPlacement - 每一個(gè)數(shù)據(jù)組的 header 的顯示位置
Top - 頂部。默認(rèn)值
Left - 左側(cè)
AreStickyGroupHeadersEnabled - 組 header 是否是固定的,即不隨組數(shù)據(jù)的滾動(dòng)而滾動(dòng)。默認(rèn)值為 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 的默認(rèn)布局控件(繼承自 Panel, 請(qǐng)參見 /Controls/LayoutControl/PanelDemo.xaml)
* FirstCacheIndex - 緩存中的第一項(xiàng)在全部數(shù)據(jù)中的索引位置
* FirstVisibleIndex - 屏幕上顯示的第一項(xiàng)在全部數(shù)據(jù)中的索引位置
* LastCacheIndex - 緩存中的最后一項(xiàng)在全部數(shù)據(jù)中的索引位置
* LastVisibleIndex - 屏幕上顯示的最后一項(xiàng)在全部數(shù)據(jù)中的索引位置
* CacheLength - 可見區(qū)外的需要緩存的數(shù)據(jù)的大小(以可見區(qū)條數(shù)大小的倍數(shù)為單位),默認(rèn)值為 4.0
* 比如當(dāng)可見區(qū)可以顯示 10 條數(shù)據(jù),CacheLength 為 4 時(shí),可見區(qū)外的需要緩存的數(shù)據(jù)的大小則為 4 * 10 = 40,也就是說整個(gè)緩存數(shù)據(jù)的大小為 10 + 4 * 10 = 50
* 實(shí)際測試發(fā)現(xiàn),可能會(huì)有一定的偏差,但是大體是準(zhǔn)確的 */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);// 構(gòu)造數(shù)據(jù)源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 數(shù)據(jù)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();
}
}
}感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“ItemsControl布局控件怎么用”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
網(wǎng)頁名稱:ItemsControl布局控件怎么用
文章起源:http://chinadenli.net/article8/goioip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、App開發(fā)、品牌網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站排名、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)