• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Windows 10 – 控件(集合类)的详细介绍

c# 搞代码 4年前 (2022-01-09) 23次浏览 已收录 0个评论

示例

1、自定义 ItemsControl(自定义 GirdView 使其每个 item 占用不同大小的空间)
Controls/CollectionControl/ItemsControlDemo/MyItemsControlDemo.xaml

<Pagex:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.MyItemsControlDemo"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><DataTemplate x:Key="ItemTemplate"><Grid Background="{Binding ColorValue}"><Grid Background="Black" VerticalAlignment="Top" Opacity="0.7"><TextBlock Text="{Binding ColorName}" /></Grid></Grid></DataTemplate><Style x:Key="ItemContainerStyle" TargetType="GridViewItem"><Setter Property="VerticalContentAlignment" Value="Stretch" /><Setter Property="HorizontalContentAlignment" Value="Stretch" /><Setter Property="Margin" Value="0" /><Setter Property="Padding" Value="0" /></Style><ItemsPanelTemplate x:Key="ItemsPanel"><VariableSizedWrapGrid MaximumRowsOrColumns="8" Orientation="Horizontal" ItemWidth="100" ItemHeight="100"  /></ItemsPanelTemplate></Page.Resources><Grid Background="Transparent" Margin="10 0 10 10"><!--使用 MyGridView 控件,其重写了 GridView 的 PrepareContainerForItemOverride() 方法,详见 MyGridView.cs--><local:MyGridView x:Name="gridView" Width="812" VerticalAlignment="Top" HorizontalAlignment="Left"  ItemTemplate="{StaticResource ItemTemplate}"  ItemContainerStyle="{StaticResource ItemContainerStyle}"  ItemsPanel="{StaticResource ItemsPanel}"                           IsItemClickEnabled="False"                           SelectionMode="None"></local:MyGridView></Grid></Page>

Controls/CollectionControl/ItemsControlDemo/MyItemsControlDemo.xaml.cs

/* * ItemsControl - 集合控件(继承自 Control, 请参见 /Controls/BaseControl/ControlDemo/) *     protected virtual void PrepareContainerForItemOverride(DependencyObject element, object item); - 为 item 准备 container 时 *         element - item 的 container *         item - item *          *  * 本例用于演示如何使 GirdView 中的每个 item 占用不同大小的空间 * 1、布局控件要使用 VariableSizedWrapGrid(利用其 RowSpan 和 ColumnSpan 来实现 item 占用不同大小的空间),需要注意的是其并非是虚拟化布局控件 * 2、自定义 GridView,并重写 ItemsControl 的 protected override void PrepareContainerForItemOverride(DependencyObject element, object item) 方法 *    然后设置每个 item 的 VariableSizedWrapGrid.RowSpan 和 VariableSizedWrapGrid.ColumnSpan */using System;using System.Collections.Generic;using System.Linq;using Windows.UI;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Media;using System.Reflection;namespace Windows10.Controls.CollectionControl.ItemsControlDemo{public sealed partial class MyItemsControlDemo : Page    {public MyItemsControlDemo()        {this.InitializeComponent();               BindData();        }private void BindData()        {            Random random = new Random();// 获取 Windows.UI.Colors 的全部数据Type type = typeof(Colors);            List<ColorModel> colors = type.GetRuntimeProperties() // GetRuntimeProperties() 在 System.Reflection 命名空间下.Select(c => new ColorModel                {                    ColorName = c.Name,                    ColorValue = new SolidColorBrush((Color)c.GetValue(null)),                    ColSpan = random.Next(1, 3), // 此对象所占网格的列合并数RowSpan = random.Next(1, 3) // 此对象所占网格的行合并数                })                .ToList();// 绑定数据gridView.ItemsSource = colors;        }    }/// <summary>/// 用于数据绑定的对象/// </summary>public class ColorModel    {public string ColorName { get; set; }public SolidColorBrush ColorValue { get; set; }// 此对象所占的网格的列合并数public int ColSpan { get; set; }// 此对象所占的网格的行合并数public int RowSpan { get; set; }    }/// <summary>/// 自定义 GridView<p style="color:transparent">本文来源gao!%daima.com搞$代*!码网1</p>,重写 ItemsControl 的 protected override void PrepareContainerForItemOverride(DependencyObject element, object item) 方法/// 用于指定 GridView 的每个 item 所占网格的列合并数和行合并数/// </summary>public class MyGridView : GridView    {protected override void PrepareContainerForItemOverride(DependencyObject element, object item)        {try{// 设置每个 item 的 VariableSizedWrapGrid.RowSpan 和 VariableSizedWrapGrid.ColumnSpan, 从而实现每个 item 占用不同大小的空间// 仅为演示用,由于这里的 ColSpan 和 RowSpan 都是随机计算的,所以可能会出现空白空间dynamic dynamicItem = item;                element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, dynamicItem.ColSpan);                element.SetValue(VariableSizedWrapGrid.RowSpanProperty, dynamicItem.RowSpan);            }catch (Exception ex)            {var ignore = ex;// 当有异常情况发生时(比如:item 没有 ColSpan 属性或 RowSpan 属性)element.SetValue(VariableSizedWrapGrid.ColumnSpanProperty, 1);                element.SetValue(VariableSizedWrapGrid.RowSpanProperty, 1);            }finally{base.PrepareContainerForItemOverride(element, item);            }        }    }}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Windows 10 – 控件(集合类)的详细介绍
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址