91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Windows 10中控件集合類的示例分析

發布時間:2021-09-17 10:33:20 來源:億速云 閱讀:131 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關Windows 10中控件集合類的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

示例

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 - 集合控件
 *     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,重寫 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);
            }
        }
    }
}

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

<Pagex:Class="Windows10.Controls.CollectionControl.ItemsControlDemo.MyItemPresenterDemo"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><Style x:Key="MyGridViewItemPresenterTemplate" TargetType="GridViewItem"><Setter Property="Background" Value="Transparent"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="GridViewItem"><!--自定義 ContentPresenter 實現類似 GridViewItemPresenter 和 ListViewItemPresenter 的效果--><local:MyItemPresenter /></ControlTemplate></Setter.Value></Setter></Style></Page.Resources><Grid Background="Transparent"><GridView x:Name="gridView" SelectionMode="Single" Margin="10 0 10 10" 
                  ItemContainerStyle="{StaticResource MyGridViewItemPresenterTemplate}"><GridView.ItemTemplate><DataTemplate><Grid Height="100" Width="100" Background="Blue"><TextBlock x:Name="lblName" Text="{Binding Name}" Foreground="Yellow" /></Grid></DataTemplate></GridView.ItemTemplate></GridView></Grid></Page>

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();
        }
    }
}

關于“Windows 10中控件集合類的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

清涧县| 灵山县| 富民县| 巴林右旗| 余江县| 江门市| 齐齐哈尔市| 郓城县| 灵丘县| 田阳县| 繁峙县| 汉川市| 桃园市| 张北县| 菏泽市| 阆中市| 教育| 临江市| 垦利县| 安龙县| 法库县| 武宁县| 兴山县| 湖州市| 方城县| 杭州市| 安平县| 黄骅市| 阜新| 镇康县| 株洲市| 宁安市| 长海县| 佛坪县| 鄢陵县| 磐石市| 江安县| 杭锦旗| 宝坻区| 叙永县| 体育|