WPF基础四:UI相关的类 (2) Panel类

目录

 

2.2) 布局控件

2.2.1) Panel类

2.2.2)Canvas类

2.2.3)DockPanel类

2.2.4)Grid类

2.2.5)StackPanel类

2.2.6)TabPanel类

2.2.7)ToolBarOverflowPanel类

2.2.8)ToolBarPanel类

2.2.9)UniformGrid类

2.2.10)VirtualizingPanel类

2.2.11)VirtualizingStackPanel类

2.2.12)WrapPanel类


2.2) 布局控件

Object->DispatcherObject->DependencyObject->Visual->UIElement->FrameworkElement->Panel


2.2.1) Panel类

用于对控件集合进行分组。

Panel是包含其他控件的控件。 您可以使用 Panel 对控件(如一组控件)的集合进行分组 RadioButton 。 与其他容器控件(如 GroupBox 控件)一样,如果 Panel 控件的 Enabled 属性设置为,则 false 中包含的控件也将被 Panel 禁用。

Panel默认情况下,控件显示时没有任何边框。 您可以使用属性提供标准或三维边框 BorderStyle ,将面板的区域与窗体上的其他区域区分开来。 由于 Panel 控件派生自 ScrollableControl 类,因此你可以使用 AutoScroll 属性来启用控件中的滚动条 Panel 。 当 AutoScroll 属性设置为时 true ,位于 Panel (中但位于其可见区域外的任何控件) ,都可以滚动到提供的滚动条。

Panel控件不显示标题。 如果需要与可以显示标题的控件类似的控件 Panel ,请参阅 GroupBox 控件。

    public abstract class Panel : FrameworkElement, IAddChild
    {
        //#########################字段###############################
        public static readonly System.Windows.DependencyProperty BackgroundProperty;
        public static readonly System.Windows.DependencyProperty IsItemsHostProperty;
        public static readonly System.Windows.DependencyProperty ZIndexProperty;

        //#########################属性###############################
        //获取或设置用于填充 Brush 的边框之间的区域的 Panel。
        public System.Windows.Media.Brush Background { get; set; }

        //获取此 UIElementCollection 的子元素的 Panel。
        public System.Windows.Controls.UIElementCollection Children { get; }

        //获取一个值,该值指示此 Panel 是否在单个维度中排列其子代。
        protected internal virtual bool HasLogicalOrientation { get; }

        //获取一个值,该值指示此 Panel 是否在单个维度中排列其子代。
        public bool HasLogicalOrientationPublic { get; }

        //获取子元素的 UIElementCollection。
        protected internal System.Windows.Controls.UIElementCollection InternalChildren { get; }

        //获取或设置一个值,该值指示这 Panel 是用户界面 (UI 的容器) 由生成的项 ItemsControl 。
        [System.ComponentModel.Bindable(false)]
        public bool IsItemsHost { get; set; }

        //获取一个枚举器,它可以循环访问此 Panel 元素的各个逻辑子元素。
        protected internal override System.Collections.IEnumerator LogicalChildren { get; }

        //如果面板支持只有一个维度的布局,则为面板的 Orientation。
        protected internal virtual System.Windows.Controls.Orientation LogicalOrientation { get; }

        //如果面板支持只有一个维度的布局,则为面板的 Orientation。
        public System.Windows.Controls.Orientation LogicalOrientationPublic { get; }

        //获取此 Visual 实例中的子 Panel 对象数。
        protected override int VisualChildrenCount { get; }

        //#########################方法###############################
        //创建一个新的 UIElementCollection。
        protected virtual System.Windows.Controls.UIElementCollection CreateUIElementCollection(System.Windows.FrameworkElement logicalParent);

        //获取指定索引位置处的此 Visual 的 Panel 子级。
        protected override System.Windows.Media.Visual GetVisualChild(int index);

        //获取给定元素的 ZIndex 属性的值。
        public static int GetZIndex(System.Windows.UIElement element);

        //指示 IsItemsHost 属性值已更改。
        protected virtual void OnIsItemsHostChanged(bool oldIsItemsHost, bool newIsItemsHost);

        //在 Panel 元素的呈现处理过程中,绘制 DrawingContext 对象的内容。
        protected override void OnRender(System.Windows.Media.DrawingContext dc);

        //当修改可见对象的 VisualCollection 时调用。
        protected internal override void OnVisualChildrenChanged(System.Windows.DependencyObject visualAdded, System.Windows.DependencyObject visualRemoved);

        //设置给定元素的 ZIndex 附加属性的值。
        public static void SetZIndex(System.Windows.UIElement element, int value);

        //确定是否应对面板的 Children 集合进行序列化
        public bool ShouldSerializeChildren();

        //#########################接口###############################
        //此类型或成员支持 Windows Presentation Foundation (WPF) 基础结构,并且不应在代码中直接使用。
        void IAddChild.AddChild(object value);

        //此类型或成员支持 Windows Presentation Foundation (WPF) 基础结构,并且不应在代码中直接使用。
        void IAddChild.AddText(string text);


        //#########################附加属性###############################                
        //获取或设置一个值,该值表示元素在 Z 平面中的显示顺序。
        //Panel.ZIndex
        //see GetZIndex, and SetZIndex
    }

2.2.2)Canvas类

定义一个区域,可在其中使用相对于 Canvas 区域的坐标以显式方式来定位子元素。

Canvas包含对象的集合 UIElement ,这些对象位于 Children 属性中。

Canvas 是无固有布局特征的唯一面板元素。

ZIndex属性确定共享同一坐标空间的子元素出现的顺序。 ZIndex一个子元素的值越大,表示此元素将显示在具有较低值的另一个子元素之上。

如果指定这些属性,则附加属性 Canvas.Top 或Canvas.Left 优先 于 Canvas.Bottom 或 Canvas.Right 属性。

Canvas 是顶级布局控件,可以用于子内容的绝对定位。 对于绘制和绘制,使用画笔,无需使用 Canvas 。

默认情况下,panel 元素不接收焦点。 若要强制 panel 元素以接收焦点,请将 Focusable 属性设置为 true 。

    public class Canvas : Panel
    {
        public static readonly System.Windows.DependencyProperty BottomProperty;
        public static readonly System.Windows.DependencyProperty LeftProperty;
        public static readonly System.Windows.DependencyProperty RightProperty;
        public static readonly System.Windows.DependencyProperty TopProperty;

        //排列 Canvas 元素的内容。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //返回给定依赖对象的 Bottom 附加属性的值。
        public static double GetBottom(System.Windows.UIElement element);

        //返回一个剪裁几何图形,它指示当 ClipToBounds 属性设置为 true 时要剪裁的区域。
        protected override System.Windows.Media.Geometry GetLayoutClip(System.Windows.Size layoutSlotSize);

        //为给定的依赖对象获取 Left 附加属性的值。
        public static double GetLeft(System.Windows.UIElement element);

        //为给定的依赖对象获取 Right 附加属性的值。
        public static double GetRight(System.Windows.UIElement element);

        //为给定的依赖对象获取 Top 附加属性的值。
        public static double GetTop(System.Windows.UIElement element);

        //测量 Canvas 的子元素,以便准备在 ArrangeOverride(Size) 处理过程中排列它们。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //设置给定的依赖对象设置上的 Bottom 附加属性值。
        public static void SetBottom(System.Windows.UIElement element, double length);

        //设置给定的依赖对象设置上的 Left 附加属性值。
        public static void SetLeft(System.Windows.UIElement element, double length);

        //设置给定的依赖对象设置上的 Right 附加属性值。
        public static void SetRight(System.Windows.UIElement element, double length);

        //设置给定的依赖对象设置上的 Top 附加属性值。
        public static void SetTop(System.Windows.UIElement element, double length);

        //获取或设置一个值,该值表示元素底部与其父 Canvas 底部之间的距离。
        //Canvas.Bottom
        //see GetBottom, and SetBottom

        //获取或设置一个值,该值表示元素左侧与其父 Canvas 左侧之间的距离。
        //Canvas.Lef
        //see GetLeft, and SetLeft

        //获取或设置一个值,该值表示元素右侧与其父 Canvas 右侧之间的距离。
        //Canvas.Right
        //see GetRight, and SetRight

        //获取或设置一个值,该值表示元素顶部与其父 Canvas 顶部之间的距离。
        //Canvas.Top
        //see GetTop, and SetTop  

    }

2.2.3)DockPanel类

定义一个区域,从中可以按相对位置水平或垂直排列各个子元素。

DockPanel包含对象的集合 UIElement ,这些对象位于 Children 属性中。

SetDock方法相对于同一容器中的其他元素更改元素位置。 对齐属性(例如 HorizontalAlignment )更改元素相对于其父元素的位置。

如果将属性设置 LastChildFill 为(默认设置),则中的 true 最后一个子元素始终会 DockPanel 填充剩余空间,而不考虑在最后一个子元素上设置的任何其他 dock 值。 若要以另一个方向停靠子元素,则必须将 LastChildFill 属性设置为 false ,并且还必须为最后一个子元素指定显式停靠方向。

默认情况下,Panel 元素不接收焦点。 若要强制 panel 元素以接收焦点,请将 Focusable 属性设置为 true 。

屏幕上的DockPanel的子元素的位置由各自子元素的Dock 属性 以及 这些子元素的相对 DockPanel顺序确定 。 因此,具有相同 Dock 属性值的一组子元素可以在屏幕上以不同方式定位,具体取决于这些子元素在 DockPanel下的顺序 。由于DockPanel依次迭代其子元素,因此子元素排序效果定位,取决于剩余空间来设置每个元素的位置。

    public class DockPanel : Panel
    {
        public static readonly System.Windows.DependencyProperty DockProperty;
        public static readonly System.Windows.DependencyProperty LastChildFillProperty;

        //获取或设置一个值,该值指示 DockPanel 中的最后一个子元素是否拉伸以填充剩余的可用空间。
        public bool LastChildFill { get; set; }

        //排列 DockPanel 元素的内容(子元素)。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //获取指定的 UIElement 的 Dock 附加属性值。
        public static System.Windows.Controls.Dock GetDock(System.Windows.UIElement element);

        //测量 DockPanel 的子元素,以便准备在 ArrangeOverride(Size) 过程中排列它们。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //将 Dock 附加属性的值设置为指定的元素。
        public static void SetDock(System.Windows.UIElement element, System.Windows.Controls.Dock dock);

        //获取或设置一个值,该值指示一个子元素在父级 DockPanel 中的位置。
        //DockPanel.Dock
        //see GetDock, and SetDock     
    }

2.2.4)Grid类

定义由列和行组成的灵活的网格区域。

在中定义的列和行 Grid 可以利用 Star 大小调整,按比例分配剩余空间。 如果 Star 选择作为行或列的高度或宽度,则该列或行会获得剩余可用空间的加权比例。 这与 Auto 不同,后者根据列或行中内容的大小均匀分配空间。

LANGUAGE (XAML) 时,此值表示为或。 在第一种情况下,行或列将接收一次可用空间,而在第二种情况下,行或列将接收两次可用空间,依此类推。 通过将此方法与 HorizontalAlignment 的值和 VerticalAlignment 的值组合在一起,可以 Stretch 按屏幕空间百分比分区布局空间。 Grid 是唯一可以采用这种方式分布空间的布局面板。

默认情况下,行和列占用的空间量最少,以容纳给定行或列中包含的任何单元内的最大内容。 例如,如果某个列中包含一个包含长词(如 "hippopotamus")的单元格,但列中的所有其他单元格都具有更小的词(如 "dog"),则该列的宽度将是最大单词 (hippopotamus) 的宽度。

可以 Grid 通过结合使用 Margin 属性和对齐属性,精确地定位的子元素。

子元素 Grid 按它们在标记或代码中出现的顺序进行绘制。 因此,当元素共享相同的坐标时,可以实现分层顺序 (也称为 z 顺序) 。

Grid 和 Table 共享一些常用功能,但每个功能都可以在适当的方案中应用,以便更好地使用其内置功能。 Grid 基于行和列索引添加元素; Table 不会。 Grid元素允许内容分层,其中有多个元素可以存在于单个单元中。 Table 不支持分层。  Grid 的子元素可相对于其 "单元格" 边界的左上角进行绝对定位。 Table 不支持此功能。 Grid 还提供比 Table 更灵活的大小调整行为。

Grid 使用 GridLength 对象定义 RowDefinition 或ColumnDefinition 的大小调整特性。 有关每个单位类型的定义,请参阅 GridUnitType 。

如果将子元素添加到 Grid 中的列,并且该列的 Width 属性设置为 Auto ,则将以无限制度量子元素。 如果正在使用,则此行为会阻止水平滚动条显示 ScrollViewer ,因为子元素被视为未绑定。 出于显示目的,将剪裁子元素,而不是滚动。

默认情况下,Panel 元素不接收焦点。 若要强制 panel 元素以接收焦点,请将 Focusable 属性设置为 true 。

    public class Grid : Panel, IAddChild
    {
        public static readonly System.Windows.DependencyProperty ColumnProperty;
        public static readonly System.Windows.DependencyProperty ColumnSpanProperty;
        public static readonly System.Windows.DependencyProperty IsSharedSizeScopeProperty;
        public static readonly System.Windows.DependencyProperty RowProperty;
        public static readonly System.Windows.DependencyProperty RowSpanProperty;
        public static readonly System.Windows.DependencyProperty ShowGridLinesProperty;

        //获取在 Grid 的实例上定义的 ColumnDefinitionCollection。
        public System.Windows.Controls.ColumnDefinitionCollection ColumnDefinitions { get; }

        //获取一个可用于迭代此 Grid 的逻辑子级的枚举数
        protected internal override System.Collections.IEnumerator LogicalChildren { get; }

        //获取在 Grid 的实例上定义的 RowDefinitionCollection。
        public System.Windows.Controls.RowDefinitionCollection RowDefinitions { get; }

        //获取或设置一个值,该值指示网格线在此 Grid 中是否可见。
        public bool ShowGridLines { get; set; }

        //获取一个表示此 Visual 实例中的 Grid 子级总数的值。
        protected override int VisualChildrenCount { get; }

        //排列 Grid 元素的内容。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //从给定的 UIElement 中获取 Column 附加属性值。
        public static int GetColumn(System.Windows.UIElement element);

        //从给定的 UIElement 中获取 ColumnSpan 附加属性值。
        public static int GetColumnSpan(System.Windows.UIElement element);

        //从给定的 UIElement 中获取 IsSharedSizeScope 附加属性值。
        public static bool GetIsSharedSizeScope(System.Windows.UIElement element);

        //从给定的 UIElement 中获取 Row 附加属性值。
        public static int GetRow(System.Windows.UIElement element);

        //从给定的 UIElement 中获取 RowSpan 附加属性值。
        public static int GetRowSpan(System.Windows.UIElement element);

        //获取指定 index 位置处的子 Visual。
        protected override System.Windows.Media.Visual GetVisualChild(int index);

        //测量 Grid 的子元素,以便准备在 ArrangeOverride(Size) 传递过程中排列它们
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //当 Grid 元素的可视子级发生更改时调用。
        protected internal override void OnVisualChildrenChanged(System.Windows.DependencyObject visualAdded, System.Windows.DependencyObject visualRemoved);

        //将 Column 附加属性的值设置为给定的 UIElement。
        public static void SetColumn(System.Windows.UIElement element, int value);

        //将 ColumnSpan 附加属性的值设置为给定的 UIElement。
        public static void SetColumnSpan(System.Windows.UIElement element, int value);

        //将 IsSharedSizeScope 附加属性的值设置为给定的 UIElement。
        public static void SetIsSharedSizeScope(System.Windows.UIElement element, bool value);

        //将 Row 附加属性的值设置为给定的 UIElement。
        public static void SetRow(System.Windows.UIElement element, int value);

        //将 RowSpan 附加属性的值设置为给定的 UIElement。
        public static void SetRowSpan(System.Windows.UIElement element, int value);

        //如果与此 ColumnDefinitionCollection 实例关联的 Grid 不为空,则返回 true。
        public bool ShouldSerializeColumnDefinitions();

        //如果与此 RowDefinitionCollection 实例关联的 Grid 不为空,则返回 true
        public bool ShouldSerializeRowDefinitions();

        //此类型或成员支持 Windows Presentation Foundation (WPF) 基础结构,并且不应在代码中直接使用。
        void IAddChild.AddChild(object value);

        //此类型或成员支持 Windows Presentation Foundation (WPF) 基础结构,并且不应在代码中直接使用。
        void IAddChild.AddText(string text);

        //Grid.Column
        //获取或设置一个值,该值表示应显示 Grid 中的子内容的列。
        //see GetColumn, and SetColumn

        //Grid.ColumnSpan
        //获取或设置一个值,该值指示 Grid 中的子内容所跨越的总列数。
        //see GetColumnSpan, and SetColumnSpan


        //Grid.IsSharedSizeScope
        //获取或设置一个值,该值指示多个 Grid 元素共享大小信息
        //see GetIsSharedSizeScope, and SetIsSharedSizeScope


        //Grid.Row
        //获取或设置一个值,该值指示应显示 Grid 中的哪个子内容行。
        //see GetRow, and SetRow

        //Grid.RowSpan
        //获取或设置一个值,该值表示在一个 Grid 中子内容所跨越的总行数。
        //see GetRowSpan, and SetRowSpan
    }

2.2.5)StackPanel类

将子元素排列成水平或垂直的一行。

StackPanel包含UIElement对象的集合  ,这些对象位于 Children 属性中。

StackPanel中包含内容的HorizontalAlignment 、VerticalAlignment的默认值都是 stretch。

默认情况下,Panel 元素不接收焦点。 若要强制 panel 元素以接收焦点,请将 Focusable 属性设置为 true 。

StackPanel 实现 IScrollInfo 接口以支持逻辑滚动。 逻辑滚动用于滚动到逻辑树中的下一个元素。 这与物理滚动相反,后者按给定方向按定义的物理增量滚动内容。 如果需要物理滚动而不是逻辑滚动,请将StackPanel 包含在 ScrollViewer 中,并将其 CanContentScroll 属性设置为 false 。

    public class StackPanel : Panel, IScrollInfo, IStackMeasure
    {
        public static readonly System.Windows.DependencyProperty OrientationProperty;

        //获取或设置一个值,该值指示 StackPanel 能否水平滚动。
        public bool CanHorizontallyScroll { get; set; }

        //获取或设置一个值,该值指示内容能否垂直滚动。
        public bool CanVerticallyScroll { get; set; }

        //获取包含盘区垂直大小的一个值。
        public double ExtentHeight { get; }

        //获取包含盘区水平大小的值。
        public double ExtentWidth { get; }

        //获取一个值,该值指示此 StackPanel 是水平方向还是垂直方向。
        protected internal override bool HasLogicalOrientation { get; }

        //获取一个值,该值包含滚动内容的水平偏移量。
        public double HorizontalOffset { get; }

        //获取一个值,该值表示 Orientation 的 StackPanel。
        protected internal override System.Windows.Controls.Orientation LogicalOrientation { get; }

        //获取或设置一个值,该值指示子元素的堆叠维度。
        public System.Windows.Controls.Orientation Orientation { get; set; }

        //获取或设置一个值,该值标识控制此 StackPanel 中的滚动行为的容器。
        public System.Windows.Controls.ScrollViewer ScrollOwner { get; set; }

        //获取包含滚动内容的垂直偏移量的值。
        public double VerticalOffset { get; }

        //获取包含内容视区垂直大小的值。
        public double ViewportHeight { get; }

        //获取包含内容视区水平大小的值。
        public double ViewportWidth { get; }

        //排列 StackPanel 元素的内容。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //将内容向下滚动一个逻辑单位。
        public void LineDown();

        //将内容向左滚动一个逻辑单位。
        public void LineLeft();

        //将内容向右滚动一个逻辑单位。
        public void LineRight();

        //将内容向上滚动一个逻辑单位。
        public void LineUp();

        //滚动到指定坐标并使该 Visual 部分可见。
        public System.Windows.Rect MakeVisible(System.Windows.Media.Visual visual, System.Windows.Rect rectangle);

        //测量 StackPanel 的子元素,以便准备在 ArrangeOverride(Size) 处理过程中排列它们。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //向下逻辑滚动内容以响应鼠标滚轮按钮的单击操作。
        public void MouseWheelDown();

        //向左逻辑滚动内容以响应鼠标滚轮按钮的单击操作。
        public void MouseWheelLeft();

        //向右逻辑滚动内容以响应鼠标滚轮按钮的单击操作。
        public void MouseWheelRight();

        //向上逻辑滚动内容以响应鼠标滚轮按钮的单击操作。
        public void MouseWheelUp();

        //将内容向下逻辑滚动一页。
        public void PageDown();

        //将内容向左逻辑滚动一页。
        public void PageLeft();

        //将内容向右逻辑滚动一页。
        public void PageRight();

        //将内容向上逻辑滚动一页。
        public void PageUp();

        //设置 HorizontalOffset 属性的值。
        public void SetHorizontalOffset(double offset);

        //设置 VerticalOffset 属性的值。
        public void SetVerticalOffset(double offset);       
    }

2.2.6)TabPanel类

处理 TabItem 上的 TabControl 对象的布局。

用作 TabPanel 中的选项卡项的项宿主 TabControl 。 它确定选项卡项的正确大小和定位,并处理多行对象的逻辑 TabItem 。

    public class TabPanel : Panel
    {
        //排列 TabPanel 对象的内容并调整其大小。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //用于重写默认剪裁。
        protected override System.Windows.Media.Geometry GetLayoutClip(System.Windows.Size layoutSlotSize);

        //需要重新测量控件时调用。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);     
    }

2.2.7)ToolBarOverflowPanel类

用来排列溢出的 ToolBar 项。

ToolBar可能没有足够的空间来显示其所有项。 发生这种情况时,ToolBar中的 items(在ToolBar中的ControlTemplate指定的)可以放置在ToolBarOverflowPanel 中。 如何在ToolBarControlTemplate的中使用ToolBarOverflowPanel,请参见 工具栏样式和模板

    public class ToolBarOverflowPanel : Panel
    {
        public static readonly System.Windows.DependencyProperty WrapWidthProperty;

        //在项溢出到下一行之前获取或设置溢出 ToolBar 的建议宽度。
        public double WrapWidth { get; set; }

        //排列 ToolBarOverflowPanel 对象的内容并调整其大小。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeBounds);

        //创建一个新的 UIElementCollection。
        protected override System.Windows.Controls.UIElementCollection CreateUIElementCollection(System.Windows.FrameworkElement logicalParent);

        //重新测量 ToolBarOverflowPanel。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);        
    }

2.2.8)ToolBarPanel类

ToolBar中排列ToolBar 项。

ToolBarPanel确定哪些 ToolBar 项目放在 ToolBar 内,哪些项必须放入溢出区。 ToolBar.OverflowMode属性确定如何在ToolBarPanel 和ToolBarOverflowPanel 之间分布项 。

    public class ToolBarPanel : StackPanel
    {
        //在 ToolBar 中排列 ToolBarPanel 项并调整其大小。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //重新测量工具栏面板。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);
    }

2.2.9)UniformGrid类

提供一种在网格(网格中的所有单元格都具有相同的大小)中排列内容的方法。

    public class UniformGrid : Panel
    {
        public static readonly System.Windows.DependencyProperty ColumnsProperty;
        public static readonly System.Windows.DependencyProperty FirstColumnProperty;
        public static readonly System.Windows.DependencyProperty RowsProperty;

        //获取或设置网格中的列数。
        public int Columns { get; set; }

        //获取或设置网格第一行中前导空白单元格的数量。
        public int FirstColumn { get; set; }

        //获取或设置网格中的行数。
        public int Rows { get; set; }

        //通过在所有子元素之间平均分配空间来定义 UniformGrid 的布局。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //通过测量所有子元素计算 UniformGrid 的期望大小。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);
    }

2.2.10)VirtualizingPanel类

为虚拟化其子数据集合的 Panel 元素提供一个框架。 这是一个抽象类。

在此上下文中,“虚拟化”是指一种技术,通过该技术,可以基于屏幕上可见的数据项从大量数据项中生成用户界面(UI)元素的子集。在内存和处理器方面,当在给定时间只有少数几个UI元素出现在屏幕上时,会产生大量的UI元素。所述的panel ,从继承VirtualizingPanel,如VirtualizingStackPanel,计算visible items和来自ItemsControl的(如ListBoxListView)的ItemContainerGenerator,只创建visible items的UI元素。

以下列表描述了 VirtualizingPanel 无法虚拟化项容器时的情况:

当 VirtualizingPanel 正在虚拟化项容器时,可能需要保存与容器关联的状态信息,而不是与数据项本身相关联的状态信息。 例如,如果某个项包含在 Expander 控件中,则该 IsExpanded 状态将绑定到项容器,而不是绑定到数据项本身。 当对 Expander 新项重复使用时,的当前值 IsExpanded 将用于新项。 此外,旧项不会保留其 IsExpanded 值。

    public abstract class VirtualizingPanel : Panel
    {
        public static readonly System.Windows.DependencyProperty CacheLengthProperty;
        public static readonly System.Windows.DependencyProperty CacheLengthUnitProperty;
        public static readonly System.Windows.DependencyProperty IsContainerVirtualizableProperty;
        public static readonly System.Windows.DependencyProperty IsVirtualizingProperty;
        public static readonly System.Windows.DependencyProperty IsVirtualizingWhenGroupingProperty;
        public static readonly System.Windows.DependencyProperty ScrollUnitProperty;
        public static readonly System.Windows.DependencyProperty VirtualizationModeProperty;

        //获取一个值,该值指示 VirtualizingPanel 是否可以虚拟化在层次结构中进行了分组或组织的项。
        public bool CanHierarchicallyScrollAndVirtualize { get; }

        //获取一个值,该值指示 VirtualizingPanel 是否可以虚拟化在层次结构中进行了分组或组织的项。
        protected virtual bool CanHierarchicallyScrollAndVirtualizeCore { get; }

        //获取一个值,该值标识了此 ItemContainerGenerator 的 VirtualizingPanel。
        public System.Windows.Controls.Primitives.IItemContainerGenerator ItemContainerGenerator { get; }

        //将指定 UIElement 添加到 InternalChildren 元素的 VirtualizingPanel 集合。
        protected void AddInternalChild(System.Windows.UIElement child);

        //在派生类中实现时,将生成位于指定索引位置的项并使之可见。
        protected internal virtual void BringIndexIntoView(int index);

        //在指定的目录位置生成项,并使其可见。
        public void BringIndexIntoViewPublic(int index);

        //获取 CacheLength 属性的值。
        public static System.Windows.Controls.VirtualizationCacheLength GetCacheLength(System.Windows.DependencyObject element);

        //获取 CacheLengthUnit 属性的值。
        public static System.Windows.Controls.VirtualizationCacheLengthUnit GetCacheLengthUnit(System.Windows.DependencyObject element);

        //获取 IsContainerVirtualizable 属性的值。
        public static bool GetIsContainerVirtualizable(System.Windows.DependencyObject element);

        //获取 IsVirtualizing 附加属性的值。
        public static bool GetIsVirtualizing(System.Windows.DependencyObject element);

        //获取 IsVirtualizingWhenGrouping 属性的值。
        public static bool GetIsVirtualizingWhenGrouping(System.Windows.DependencyObject element);

        //返回指定项相对于 VirtualizingPanel 的位置。
        public double GetItemOffset(System.Windows.UIElement child);

        //返回指定项相对于 VirtualizingPanel 的位置。
        protected virtual double GetItemOffsetCore(System.Windows.UIElement child);

        //获取 ScrollUnit 属性的值。
        public static System.Windows.Controls.ScrollUnit GetScrollUnit(System.Windows.DependencyObject element);

        //返回指定对象的 VirtualizationMode 附加属性。
        public static System.Windows.Controls.VirtualizationMode GetVirtualizationMode(System.Windows.DependencyObject element);

        //将指定 UIElement 添加到 InternalChildren 元素的 VirtualizingPanel 集合中的指定索引位置处。
        protected void InsertInternalChild(int index, System.Windows.UIElement child);

        //当 Panel 基类清除了子元素的集合时调用。
        protected virtual void OnClearChildren();

        //当与此 Items 的 ItemsControl 关联的 Panel 集合发生更改时调用。
        protected virtual void OnItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs args);

        //移除 InternalChildren 集合中的子元素。
        protected void RemoveInternalChildRange(int index, int range);

        //设置 CacheLength 附加属性。
        public static void SetCacheLength(System.Windows.DependencyObject element, System.Windows.Controls.VirtualizationCacheLength value);

        //设置 CacheLengthUnit 附加属性。
        public static void SetCacheLengthUnit(System.Windows.DependencyObject element, System.Windows.Controls.VirtualizationCacheLengthUnit value);

        //设置 IsContainerVirtualizable 附加属性。
        public static void SetIsContainerVirtualizable(System.Windows.DependencyObject element, bool value);

        //设置 IsVirtualizingProperty 附加属性的值。
        public static void SetIsVirtualizing(System.Windows.DependencyObject element, bool value);

        //设置 IsVirtualizingWhenGrouping 附加属性。
        public static void SetIsVirtualizingWhenGrouping(System.Windows.DependencyObject element, bool value);

        //设置 ScrollUnit 附加属性。
        public static void SetScrollUnit(System.Windows.DependencyObject element, System.Windows.Controls.ScrollUnit value);

        //在指定对象上设置 VirtualizationMode 附加属性。
        public static void SetVirtualizationMode(System.Windows.DependencyObject element, System.Windows.Controls.VirtualizationMode value);

        //返回指示更改的项是否在 ItemsControl 影响该面板的布局的值。
        public bool ShouldItemsChangeAffectLayout(bool areItemChangesLocal, System.Windows.Controls.Primitives.ItemsChangedEventArgs args);

        //返回指示更改的项是否在 ItemsControl 影响该面板的布局的值。
        protected virtual bool ShouldItemsChangeAffectLayoutCore(bool areItemChangesLocal, System.Windows.Controls.Primitives.ItemsChangedEventArgs args);

        //VirtualizingPanel.CacheLength
        //获取或设置虚拟化 VirtualizingPanel 时,视区前后缓存的大小。
        //see GetCacheLength, and SetCacheLength

        //VirtualizingPanel.CacheLengthUnit
        //获取或设置 CacheLength 的属性使用的单位类型。
        //see GetCacheLengthUnit, and SetCacheLengthUnit

        //VirtualizingPanel.IsContainer
        //获取或设置一个值,该值指示此 VirtualizingPanel 是否应当虚拟化一个项。
        //see GetIsContainerVirtualizable, and SetIsContainerVirtualizable

        //VirtualizingPanel.IsVirtualizing
        //获取或设置一个值,该值指示此 VirtualizingPanel 正在虚拟化其子集合。
        //see GetIsVirtualizing, and SetIsVirtualizing

        //VirtualizingPanel.IsVirtualizingWhenGrouping
        //获取或设置指示此 VirtualizingPanel 在显示组时是否虚拟化其集合中的项的值
        //see GetIsVirtualizingWhenGrouping, and SetIsVirtualizingWhenGrouping

        //VirtualizingPanel.ScrollUnit
        //在集合或者像素中获取指示滚动是否被计量为项的值
        //see GetScrollUnit, and SetScrollUnit

        //VirtualizingPanel.VirtualizationMode
        //获取或设置 ItemsControl 中的面板虚拟化其子项的方式。
        //see GetVirtualizationMode, and SetVirtualizationMode
    }

2.2.11)VirtualizingStackPanel类

在水平或垂直的一行中排列并显示内容。

在水平或垂直方向上的一行上排列和虚拟化内容。

标准布局系统创建项目容器,并为与列表控件关联的每个项目计算布局。单词“虚拟化”指的是一种技术,通过该技术,可从大量数据项中生成用户界面(UI)元素的子集,基于这些项在屏幕上可见。当屏幕上可能只有几个元素时,生成许多UI元素可能会对应用程序的性能产生不利影响。VirtualizingStackPanel计算可见项目的数量,并与来自ItemsControl的(如ListBox或ListView)的ItemContainerGenerator来创建仅针对可见项的UI元素。

仅当面板中包含的项目控件创建其自己的项目容器时,才会在StackPanel中进行虚拟化。您可以通过使用数据绑定来确保发生这种情况。在创建项目容器并将其添加到项目控件的方案中,VirtualizingStackPanelStackPanel相比没有任何性能优势。

VirtualizingStackPanel 是元素 ListBox 的默认项宿主。 默认情况下, VirtualizingStackPanel.IsVirtualizing 附加属性设置为 true 。

将 VirtualizingStackPanel.IsVirtualizing 附加属性设置为时 false , VirtualizingStackPanel的 行为与普通相同 StackPanel 。

public class VirtualizingStackPanel : VirtualizingPanel, IScrollInfo, IStackMeasure
    {
        public static readonly System.Windows.RoutedEvent CleanUpVirtualizedItemEvent;
        public static readonly System.Windows.DependencyProperty IsVirtualizingProperty;
        public static readonly System.Windows.DependencyProperty OrientationProperty;
        public static readonly System.Windows.DependencyProperty VirtualizationModeProperty;

        //获取指示 VirtualizingStackPanel 能否虚拟化层次结构中分组或组织的项的值。
        protected override bool CanHierarchicallyScrollAndVirtualizeCore { get; }

        //获取或设置一个值,该值指示 VirtualizingStackPanel 能否水平滚动。
        public bool CanHorizontallyScroll { get; set; }

        //获取或设置一个值,该值指示内容能否垂直滚动。
        public bool CanVerticallyScroll { get; set; }

        //获取包含盘区垂直大小的一个值。
        public double ExtentHeight { get; }

        //获取包含盘区水平大小的值。
        public double ExtentWidth { get; }

        //获取一个值,该值指示此 VirtualizingStackPanel 是水平方向还是垂直方向。
        protected internal override bool HasLogicalOrientation { get; }

        //获取一个值,该值包含滚动内容的水平偏移量。
        public double HorizontalOffset { get; }

        //获取一个值,该值表示 Orientation 的 VirtualizingStackPanel。
        protected internal override System.Windows.Controls.Orientation LogicalOrientation { get; }

        //获取或设置一个值,该值描述堆叠内容的水平或垂直方向。
        public System.Windows.Controls.Orientation Orientation { get; set; }

        //获取或设置一个值,该值标识控制此 VirtualizingStackPanel 中的滚动行为的容器。
        public System.Windows.Controls.ScrollViewer ScrollOwner { get; set; }

        //获取包含滚动内容的垂直偏移量的值。
        public double VerticalOffset { get; }

        //获取包含内容视区垂直大小的值。
        public double ViewportHeight { get; }

        //获取包含内容视区水平大小的值。
        public double ViewportWidth { get; }

        //添加 CleanUpVirtualizedItem 附加事件的事件处理程序。
        public static void AddCleanUpVirtualizedItemHandler(System.Windows.DependencyObject element, System.Windows.Controls.CleanUpVirtualizedItemEventHandler handler);

        //排列 VirtualizingStackPanel 元素的内容。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //在指定索引位置生成项并使其可见。
        protected internal override void BringIndexIntoView(int index);

        //返回指定项相对于 VirtualizingStackPanel 的位置。
        protected override double GetItemOffsetCore(System.Windows.UIElement child);

        //将内容向下滚动一个逻辑单位
        public virtual void LineDown();

        //将内容向左滚动一个逻辑单位。
        public virtual void LineLeft();

        //将内容向右滚动一个逻辑单位。
        public virtual void LineRight();

        //将内容向上滚动一个逻辑单位。
        public virtual void LineUp();

        //滚动到指定坐标并使该 Visual 部分可见。
        public System.Windows.Rect MakeVisible(System.Windows.Media.Visual visual, System.Windows.Rect rectangle);

        //测量 VirtualizingStackPanel 的子元素,以便准备在 ArrangeOverride(Size) 处理过程中排列它们。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //向下逻辑滚动内容以响应鼠标滚轮按钮的向下单击操作。
        public virtual void MouseWheelDown();

        //向左逻辑滚动内容以响应鼠标滚轮按钮的向左单击操作。
        public virtual void MouseWheelLeft();

        //向右逻辑滚动内容以响应鼠标滚轮按钮的右击操作。
        public virtual void MouseWheelRight();

        //向上逻辑滚动内容以响应鼠标滚轮按钮的向上单击操作。
        public virtual void MouseWheelUp();

        //当重新虚拟化由 VirtualizingStackPanel 承载的项时调用。
        protected virtual void OnCleanUpVirtualizedItem(System.Windows.Controls.CleanUpVirtualizedItemEventArgs e);

        //当 Panel 基类清除了子元素的集合时调用。
        protected override void OnClearChildren();

        //当与此 Items 的 ItemsControl 关联的 Panel 集合发生更改时调用。
        protected override void OnItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs args);

        //当视区偏移量随用户滚动内容而发生变化时调用。
        protected virtual void OnViewportOffsetChanged(System.Windows.Vector oldViewportOffset, System.Windows.Vector newViewportOffset);

        //在视区大小更改时调用。
        protected virtual void OnViewportSizeChanged(System.Windows.Size oldViewportSize, System.Windows.Size newViewportSize);

        //将内容向下滚动一页。
        public virtual void PageDown();

        //将内容向左滚动一页。
        public virtual void PageLeft();

        //将内容向右滚动一页。
        public virtual void PageRight();

        //将内容向上滚动一页。
        public virtual void PageUp();

        //移除 CleanUpVirtualizedItem 附加事件的事件处理程序。
        public static void RemoveCleanUpVirtualizedItemHandler(System.Windows.DependencyObject element, System.Windows.Controls.CleanUpVirtualizedItemEventHandler handler);

        //设置 HorizontalOffset 属性的值。
        public void SetHorizontalOffset(double offset);

        //设置 VerticalOffset 属性的值。
        public void SetVerticalOffset(double offset);

        //返回指示更改的项是否在 ItemsControl 影响该面板的布局的值。
        protected override bool ShouldItemsChangeAffectLayoutCore(bool areItemChangesLocal, System.Windows.Controls.Primitives.ItemsChangedEventArgs args);

        //VirtualizingStackPanel.CleanUpVirtualizedItem
        //当由与此 ItemsControl 实例关联的 VirtualizingStackPanel 重新虚拟化项时发生
        //see AddCleanUpVirtualizedItemHandler, and RemoveCleanUpVirtualizedItemHandler
    }

2.2.12)WrapPanel类

按从左到右的顺序位置定位子元素,在包含框的边缘处将内容切换到下一行。 后续排序按照从上至下或从右至左的顺序进行,具体取决于 Orientation 属性的值。

WrapPanel包含UIElement对象的集合  ,这些对象位于 Children 属性中。

 WrapPanel 的所有子元素都接收ItemWidth 与ItemHeight大小相乘的布局分区 。

    public class WrapPanel : Panel
    {
        public static readonly System.Windows.DependencyProperty ItemHeightProperty;
        public static readonly System.Windows.DependencyProperty ItemWidthProperty;
        public static readonly System.Windows.DependencyProperty OrientationProperty;

        //获取或设置一个值,该值指定 WrapPanel 中所含全部项的高度
        public double ItemHeight { get; set; }

        //获取或设置一个值,该值指定 WrapPanel 中所含全部项的宽度。
        public double ItemWidth { get; set; }

        //获取或设置一个值,该值指定子内容的排列方向。
        public System.Windows.Controls.Orientation Orientation { get; set; }

        //排列 WrapPanel 元素的内容。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize);

        //测量 WrapPanel 的子元素,以便准备在 ArrangeOverride(Size) 处理过程中排列它们。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);      
    }

参考微软:

https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.controls?view=net-5.0

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值