0

0

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

零下一度

零下一度

发布时间:2017-06-26 15:40:03

|

2443人浏览过

|

来源于php中文网

原创

示例

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

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 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;
        }
    }/// /// 用于数据绑定的对象/// public class ColorModel
    {public string ColorName { get; set; }public SolidColorBrush ColorValue { get; set; }// 此对象所占的网格的列合并数public int ColSpan { get; set; }// 此对象所占的网格的行合并数public int RowSpan { get; set; }
    }/// /// 自定义 GridView,重写 ItemsControl 的 protected override void PrepareContainerForItemOverride(DependencyObject element, object item) 方法/// 用于指定 GridView 的每个 item 所占网格的列合并数和行合并数/// 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);
            }
        }
    }
}


2、自定义 ContentPresenter 实现类似 GridViewItemPresenter 和 ListViewItemPresenter 的效果
Controls/CollectionControl/ItemsControlDemo/MyItemPresenter.cs

/*
 * 自定义 ContentPresenter 实现类似 GridViewItemPresenter 和 ListViewItemPresenter 的效果 */using System;using Windows.Foundation;using Windows.UI;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Media.Animation;using Windows.UI.Xaml.Shapes;namespace Windows10.Controls.CollectionControl.ItemsControlDemo
{class MyItemPresenter : ContentPresenter
    {
        Panel _container = null; // item 的容器(即在 DataTemplate 中定义的根元素,在示例 MyItemPresenterDemo.xaml 中用的是 Grid)Rectangle _pointerOverBorder = null; // 鼠标经过 item 时覆盖在 item 上的 rectangleRectangle _focusVisual = null; // 选中 item 时覆盖在 item 上的 rectangleStoryboard _pointerDownStoryboard = null; // 鼠标按下时的动画Storyboard _pointerUpStoryboard = null; // 鼠标抬起时的动画public MyItemPresenter()
            : base()
        {base.Margin = new Thickness(10);
        }// override OnApplyTemplate() - 应用控件模板时调用protected override void OnApplyTemplate()
        {base.OnApplyTemplate();

            _container = (Panel)VisualTreeHelper.GetChild(this, 0);
        }// override GoToElementStateCore() - VisualState 转换时调用(此方法仅在自定义 ContentPresenter 并将其应用于 GridView 或 ListView 的 ItemContainerStyle 时才会被调用)//     stateName - VisualState 的名字//     useTransitions - 是否使用 VisualTransition 过渡效果protected override bool GoToElementStateCore(string stateName, bool useTransitions)
        {base.GoToElementStateCore(stateName, useTransitions);switch (stateName)
            {// 正常状态case "Normal":
                    HidePointerOverVisuals();
                    HideFocusVisuals();if (useTransitions)
                    {
                        StopPointerDownAnimation();
                    }break;// 选中状态case "Selected":case "PointerFocused":
                    ShowFocusVisuals();if (useTransitions)
                    {
                        StopPointerDownAnimation();
                    }break;// 取消选中状态case "Unfocused":
                    HideFocusVisuals();break;// 鼠标经过状态case "PointerOver":
                    ShowPointerOverVisuals();if (useTransitions)
                    {
                        StopPointerDownAnimation();
                    }break;// 鼠标点击状态case "Pressed":case "PressedSelected":if (useTransitions)
                    {
                        StartPointerDownAnimation();
                    }break;default: break;
            }return true;
        }private void StartPointerDownAnimation()
        {if (_pointerDownStoryboard == null)
                CreatePointerDownStoryboard();

            _pointerDownStoryboard.Begin();
        }private void StopPointerDownAnimation()
        {if (_pointerUpStoryboard == null)
                CreatePointerUpStoryboard();

            _pointerUpStoryboard.Begin();
        }private void ShowFocusVisuals()
        {if (!FocusElementsAreCreated())
                CreateFocusElements();

            _focusVisual.Opacity = 1;
        }private void HideFocusVisuals()
        {if (FocusElementsAreCreated())
                _focusVisual.Opacity = 0;
        }private void ShowPointerOverVisuals()
        {if (!PointerOverElementsAreCreated())
                CreatePointerOverElements();

            _pointerOverBorder.Opacity = 1;
        }private void HidePointerOverVisuals()
        {if (PointerOverElementsAreCreated())
                _pointerOverBorder.Opacity = 0;
        }private void CreatePointerDownStoryboard()
        {/* * 用这种方式为 item 实现鼠标按下的效果会报错(Attempted to read or write protected memory. This is often an indication that other memory is corrupt.),不知道为什么
             * PointerDownThemeAnimation pointerDownAnimation = new PointerDownThemeAnimation();
             * Storyboard.SetTarget(pointerDownAnimation, _container);
             * Storyboard pointerDownStoryboard = new Storyboard();
             * pointerDownStoryboard.Children.Add(pointerDownAnimation);             */DoubleAnimation da1 = new DoubleAnimation()
            {
                To = 0.9,
                Duration = TimeSpan.FromMilliseconds(100)
            };
            DoubleAnimation da2 = new DoubleAnimation()
            {
                To = 0.9,
                Duration = TimeSpan.FromMilliseconds(100)
            };
            Storyboard.SetTarget(da1, _container);
            Storyboard.SetTargetProperty(da1, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)");
            Storyboard.SetTarget(da2, _container);
            Storyboard.SetTargetProperty(da2, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)");if (!(_container.RenderTransform is TransformGroup))
            {
                TransformGroup Group = new TransformGroup();
                Group.Children.Add(new ScaleTransform());
                _container.RenderTransform = Group;
                _container.RenderTransformOrigin = new Point(0.5, 0.5);
            }

            _pointerDownStoryboard = new Storyboard();
            _pointerDownStoryboard.Children.Add(da1);
            _pointerDownStoryboard.Children.Add(da2);
            _pointerDownStoryboard.Begin();
        }private void CreatePointerUpStoryboard()
        {
            DoubleAnimation da1 = new DoubleAnimation()
            {
                To = 1,
                Duration = TimeSpan.FromMilliseconds(100)
            };
            DoubleAnimation da2 = new DoubleAnimation()
            {
                To = 1,
                Duration = TimeSpan.FromMilliseconds(100)
            };
            Storyboard.SetTarget(da1, _container);
            Storyboard.SetTargetProperty(da1, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)");
            Storyboard.SetTarget(da2, _container);
            Storyboard.SetTargetProperty(da2, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)");if (!(_container.RenderTransform is TransformGroup))
            {
                TransformGroup Group = new TransformGroup();
                Group.Children.Add(new ScaleTransform());
                _container.RenderTransform = Group;
                _container.RenderTransformOrigin = new Point(0.5, 0.5);
            }

            _pointerUpStoryboard = new Storyboard();
            _pointerUpStoryboard.Children.Add(da1);
            _pointerUpStoryboard.Children.Add(da2);
            _pointerUpStoryboard.Begin();
        }private void CreatePointerOverElements()
        {
            _pointerOverBorder = new Rectangle();
            _pointerOverBorder.IsHitTestVisible = false;
            _pointerOverBorder.Opacity = 0;// 这里把颜色写死了,仅为演示用,实际写的时候要摘出来写成依赖属性_pointerOverBorder.Fill = new SolidColorBrush(Color.FromArgb(0x50, 0x50, 0x50, 0x50));

            _container.Children.Insert(_container.Children.Count, _pointerOverBorder);
        }private void CreateFocusElements()
        {
            _focusVisual = new Rectangle();
            _focusVisual.IsHitTestVisible = false;
            _focusVisual.Height = 10;
            _focusVisual.VerticalAlignment = VerticalAlignment.Bottom;// 这里把颜色写死了,仅为演示用,实际写的时候要摘出来写成依赖属性_focusVisual.Fill = new SolidColorBrush(Color.FromArgb(0xff, 0xff, 0x0, 0x0));

            _container.Children.Insert(0, _focusVisual);
        }private bool FocusElementsAreCreated()
        {return _focusVisual != null;
        }private bool PointerOverElementsAreCreated()
        {return _pointerOverBorder != null;
        }
    }
}

Controls/CollectionControl/ItemsControlDemo/MyItemPresenterDemo.xaml

Trickle AI
Trickle AI

多功能零代码AI应用开发平台

下载

Controls/CollectionControl/ItemsControlDemo/MyItemPresenterDemo.xaml.cs

/*
 * 本例用于演示如何自定义 ContentPresenter 实现类似 GridViewItemPresenter 和 ListViewItemPresenter 的效果 */using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;using Windows10.Common;namespace Windows10.Controls.CollectionControl.ItemsControlDemo
{public sealed partial class MyItemPresenterDemo : Page
    {public MyItemPresenterDemo()
        {this.InitializeComponent();
        }protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            gridView.ItemsSource = TestData.GetEmployees();
        }
    }
}



OK
[源码下载]

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

10

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

29

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

13

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

8

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

55

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.7万人学习

Excel 教程
Excel 教程

共162课时 | 13.1万人学习

PHP基础入门课程
PHP基础入门课程

共33课时 | 2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号