WPF基础四:UI的相关类 (11)Decorator类

目录

2.11 Decorator

2.11.1 Decorator

2.11.2 ButtonChrome

2.11.3 ClassicBorderDecorator

2.11.4 ListBoxChrome

2.11.5 SystemDropShadowChrome

2.11.6 Border

2.11.7 InkPresenter

2.11.8 BulletDecorator

2.11.9 Viewbox

2.11.10 Adorner


2.11 Decorator


2.11.1 Decorator

提供在单个子元素(如 Border 或 Viewbox)上或周围应用效果的元素的基类。

Decorator包含 UIElement 由属性指定的单个 Child 。 Decorator影响或修饰的 UIElement 。 Decorator可能有也可能没有外观。 例如,会在 Border 控件周围放置一个矩形,但不会 Viewbox 显示视觉对象。

    public class Decorator : FrameworkElement, IAddChild
    {
        //获取或设置 Decorator 的单一子元素。
        public virtual System.Windows.UIElement Child { get; set; }

        //获取一个可用于循环访问 Decorator 的逻辑子元素的枚举器。
        protected internal override System.Collections.IEnumerator LogicalChildren { get; }

        //获取一个值,该值等于 Decorator 的此实例的可视子元素数。
        protected override int VisualChildrenCount { get; }

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

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

        //测量 Decorator 的子元素,以准备在 ArrangeOverride(Size) 传递期间对其进行排列。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

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

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

    }

2.11.2 ButtonChrome

为 Button 元素创建特定于主题的外观。

的实际外观取决于 Button 用户系统上活动的主题。 此类的属性使 .NET 可以基于当前主题设置外观。

在 XAML 用法中, ButtonChrome 对象元素还可以具有子内容,该内容可以是设置属性的单个内容 UIElement Child 。

XAML 对象元素用法

<theme:ButtonChrome .../>

XAML 值

theme:Microsoft.Windows.ThemesCLR 命名空间的 xmlns 前缀。 通常,xmlns 前缀和映射在 XAML 根元素中定义 (未显示) 。

    public sealed class ButtonChrome : System.Windows.Controls.Decorator
    {
        public static readonly System.Windows.DependencyProperty BackgroundProperty;
        public static readonly System.Windows.DependencyProperty BorderBrushProperty;
        public static readonly System.Windows.DependencyProperty FillProperty;
        public static readonly System.Windows.DependencyProperty RenderDefaultedProperty;
        public static readonly System.Windows.DependencyProperty RenderMouseOverProperty;
        public static readonly System.Windows.DependencyProperty RenderPressedProperty;
        public static readonly System.Windows.DependencyProperty RoundCornersProperty;
        public static readonly System.Windows.DependencyProperty ThemeColorProperty;

        //获取或设置用于填充 Button 背景的画笔。
        public System.Windows.Media.Brush Background { get; set; }

        //获取或设置用于绘制 Button 的外边框的画笔。
        public System.Windows.Media.Brush BorderBrush { get; set; }

        //获取或设置用于绘制 Button 的内部颜色的画笔。
        public System.Windows.Media.Brush Fill { get; set; }

        //获取或设置一个值,该值指示 Button 是否具有窗体上默认按钮的外观。
        public bool RenderDefaulted { get; set; }

        //获取或设置一个值,该值指示 Button 上面是否好像具有鼠标。
        public bool RenderMouseOver { get; set; }

        //获取或设置一个值,该值指示 Button 看上去是否处于按下状态。
        public bool RenderPressed { get; set; }

        //获取或设置一个值,该值指示 Button 是否具有圆角。
        public bool RoundCorners { get; set; }

        //获取或设置主题颜色。
        public Microsoft.Windows.Themes.ThemeColor ThemeColor { get; set; }

    }

2.11.3 ClassicBorderDecorator

创建 Decorator 类型的特定于主题的外观,以与传统型主题一起使用。

若要实现经典外观,请 ClassicBorderBrush 将指定为 BorderBrush 。 任何其他画笔都将删除经典外观,并呈现的边框 BorderThickness (元素的大小写除外 TabControl ,这种情况总是以经典外观呈现)。

XAML 对象元素用法

<theme:ClassicBorderDecorator ...>  
  singleChild  
</theme:ClassicBorderDecorator>  

XAML 值

theme:
Microsoft.Windows.ThemesCLR 命名空间的 xmlns 前缀。 通常,xmlns 前缀和映射在 XAML 根元素中定义 (未显示) 。

singleChild
单个对象元素子级,表示边框绘制的对象。 此对象必须是 UIElement 。 典型的子元素为固定 UIElement (例如 Path) ,或者是组合中的表示器或其他修饰器。

    public sealed class ClassicBorderDecorator : System.Windows.Controls.Decorator
    {
        public static readonly System.Windows.DependencyProperty BackgroundProperty;
        public static readonly System.Windows.DependencyProperty BorderBrushProperty;
        public static readonly System.Windows.DependencyProperty BorderStyleProperty;
        public static readonly System.Windows.DependencyProperty BorderThicknessProperty;


        //public System.Windows.Media.Brush Background { get; set; }
        public System.Windows.Media.Brush Background { get; set; }

        //public System.Windows.Media.Brush BorderBrush { get; set; }
        public System.Windows.Media.Brush BorderBrush { get; set; }

        //获取或设置用于绘制元素外边框的 ClassicBorderStyle。
        public Microsoft.Windows.Themes.ClassicBorderStyle BorderStyle { get; set; }

        //获取或设置边框的宽度。
        public System.Windows.Thickness BorderThickness { get; set; }

        //获取用于绘制边框的画笔。
        public static System.Windows.Media.Brush ClassicBorderBrush { get; }
    }

2.11.4 ListBoxChrome

为 ListBox 元素创建特定于主题的外观。

    public sealed class ListBoxChrome : System.Windows.Controls.Decorator
    {
        public static readonly System.Windows.DependencyProperty BackgroundProperty;
        public static readonly System.Windows.DependencyProperty BorderBrushProperty;
        public static readonly System.Windows.DependencyProperty BorderThicknessProperty;
        public static readonly System.Windows.DependencyProperty RenderFocusedProperty;
        public static readonly System.Windows.DependencyProperty RenderMouseOverProperty;

        //获取或设置用于填充 ListBox 背景的画笔。
        public System.Windows.Media.Brush Background { get; set; }

        //获取或设置用于绘制 ListBox 的外边框的画笔。
        public System.Windows.Media.Brush BorderBrush { get; set; }

        //获取或设置 ListBox 的边框粗细。
        public System.Windows.Thickness BorderThickness { get; set; }

        //获取或设置一个值,该值指示 ListBox 看上去是否好像具有键盘焦点。
        public bool RenderFocused { get; set; }

        //获取或设置一个值,该值指示 ListBox 上面是否好像具有鼠标。
        public bool RenderMouseOver { get; set; }
    }

2.11.5 SystemDropShadowChrome

创建投影效果的特定于主题的外观。

投影效果用于创建元素内容的实体剪影。 这将创建内容为浮动和转换阴影的错觉。

XAML 对象元素用法

<theme:ListBoxChrome ...>
  singleChild
</theme:ListBoxChrome>

XAML 值

theme:Microsoft.Windows.ThemesCLR 命名空间的 xmlns 前缀。 通常,xmlns 前缀和映射在 XAML 根元素中定义 (未显示) 。

singleChild 单个对象元素子级,表示修饰器绘制到的对象。 此对象必须是 UIElement 。

    public sealed class ListBoxChrome : System.Windows.Controls.Decorator
    {
        public static readonly System.Windows.DependencyProperty BackgroundProperty;
        public static readonly System.Windows.DependencyProperty BorderBrushProperty;
        public static readonly System.Windows.DependencyProperty BorderThicknessProperty;
        public static readonly System.Windows.DependencyProperty RenderFocusedProperty;
        public static readonly System.Windows.DependencyProperty RenderMouseOverProperty;

        //获取或设置用于填充 ListBox 背景的画笔。
        public System.Windows.Media.Brush Background { get; set; }

        //获取或设置用于绘制 ListBox 的外边框的画笔。
        public System.Windows.Media.Brush BorderBrush { get; set; }

        //获取或设置 ListBox 的边框粗细。
        public System.Windows.Thickness BorderThickness { get; set; }

        //获取或设置一个值,该值指示 ListBox 看上去是否好像具有键盘焦点。
        public bool RenderFocused { get; set; }

        //获取或设置一个值,该值指示 ListBox 上面是否好像具有鼠标。
        public bool RenderMouseOver { get; set; }
    }

2.11.6 Border

在另一个元素四周绘制边框和/或背景。

Border 只能有一个子级。 若要显示多个子元素,需要在父元素中放置一个额外的 Panel 元素 Border 。 然后,你可以在该元素中放置子元素 Panel 。

如果要在内容周围显示边框,则必须将元素放入父 Border 元素中。

    public class Border : Decorator
    {
        public static readonly System.Windows.DependencyProperty BackgroundProperty;
        public static readonly System.Windows.DependencyProperty BorderBrushProperty;
        public static readonly System.Windows.DependencyProperty BorderThicknessProperty;
        public static readonly System.Windows.DependencyProperty CornerRadiusProperty;
        public static readonly System.Windows.DependencyProperty PaddingProperty;

        //获取或设置 Brush,它填充 Border 边界之间的区域。
        public System.Windows.Media.Brush Background { get; set; }

        //获取或设置用于绘制外部边框颜色的 Brush。
        public System.Windows.Media.Brush BorderBrush { get; set; }

        //获取或设置 Border 的相对 Thickness。
        public System.Windows.Thickness BorderThickness { get; set; }

        //获取或设置一个值,该值表示将 Border 的角倒圆的程度。
        public System.Windows.CornerRadius CornerRadius { get; set; }

        //获取或设置描述 Thickness 及其子元素之间的空间量的 Border 值。
        public System.Windows.Thickness Padding { get; set; }

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

        //在 Border 处理过程中排列 ArrangeOverride(Size) 的子元素之前,对这些子元素进行测量。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

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

    }

2.11.7 InkPresenter

在图面上呈现墨迹。

墨迹呈现以两种方式出现:动态和静态。 当墨迹写入墨迹图面时,动态呈现会出现:在收集笔画时,将呈现笔画。 在收集笔划数据并将其附加到新笔划后,会发生静态呈现。

若要使用动态呈现墨迹 InkPresenter ,请 RootVisual DynamicRenderer InkPresenter 使用 AttachVisuals 方法将的属性附加到。 若要以静态方式呈现墨迹,请将笔划对象添加到 Strokes 属性。

    public class InkPresenter : System.Windows.Controls.Decorator
    {
        public static readonly System.Windows.DependencyProperty StrokesProperty;

        //获取或设置 InkPresenter 显示的笔划。
        public System.Windows.Ink.StrokeCollection Strokes { get; set; }

        //获取一个值,该值等于 Decorator 的此实例的可视子元素数。
        protected override int VisualChildrenCount { get; }

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

        //将 DynamicRenderer 的视觉效果附加到 InkPresenter。
        public void AttachVisuals(System.Windows.Media.Visual visual, System.Windows.Ink.DrawingAttributes drawingAttributes);

        //从 DynamicRenderer 中分离 InkPresenter 的视觉效果。
        public void DetachVisuals(System.Windows.Media.Visual visual);

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

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

        //测量 Decorator 的子元素,以准备在 ArrangeOverride(Size) 传递期间对其进行排列。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //提供 InkPresenterAutomationPeer 此控件的适当实现,作为 WPF 基础结构的一部分。
        protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer();    //

    }

2.11.8 BulletDecorator

表示一个布局控件,该控件将项目符号与另一个可视对象对齐。

BulletDecorator 具有两个内容属性: Bullet 和 Child 。 Bullet属性定义 UIElement 要用作项目符号的。 Child属性定义一个 UIElement ,它与项目符号在视觉上对齐。

Bullet当对象为时,始终与第一行文本对齐 Child TextBlock 。 如果 Child 对象不是 TextBlock ,则与 Bullet 对象的中心对齐 Child 。 有关布局的详细信息 BulletDecorator ,请参阅 如何:创建用作 bulletdecorator

下图显示了控件的示例 BulletDecorator 。

3 个 BulletDecorator:CheckBox、RadioButton、TextBox

    public class BulletDecorator : System.Windows.Controls.Decorator
    {
        public static readonly System.Windows.DependencyProperty BackgroundProperty;


        //获取或设置 BulletDecorator 控件的背景色。
        public System.Windows.Media.Brush Background { get; set; }

        //获取或设置要在 BulletDecorator 中用作项目符号的对象。
        public System.Windows.UIElement Bullet { get; set; }

        //获取 BulletDecorator 控件的逻辑子元素的枚举数。
        protected internal override System.Collections.IEnumerator LogicalChildren { get; }

        //获取 BulletDecorator 控件的可视化子元素的数目。
        protected override int VisualChildrenCount { get; }

        //重写 BulletDecorator 控件的默认内容排列行为。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeSize);

        //获取位于指定索引处的子元素。
        protected override System.Windows.Media.Visual GetVisualChild(int index);

        //重写 BulletDecorator 控件的对象的默认测量行为。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //呈现 BulletDecorator 控件的内容。
        protected override void OnRender(System.Windows.Media.DrawingContext dc);
    }

2.11.9 Viewbox

定义一个内容修饰器,以便拉伸或缩放单一子项使其填满可用的控件。

Viewbox只能有一个 Child 。 如果添加其他 Child ,则会 ArgumentException 在运行时引发。

    public class Viewbox : Decorator
    {
        public static readonly System.Windows.DependencyProperty StretchDirectionProperty;
        public static readonly System.Windows.DependencyProperty StretchProperty;

        //获取或设置 Viewbox 元素的单一子元素。
        public override System.Windows.UIElement Child { get; set; }

        //获取一个可迭代此 Viewbox 元素的逻辑子元素的枚举器。
        protected internal override System.Collections.IEnumerator LogicalChildren { get; }

        //获取或设置 ViewboxStretch 模式,该模式确定内容适应可用空间的方式。
        public System.Windows.Media.Stretch Stretch { get; set; }

        //获取或设置 StretchDirection,它确定缩放如何应用 Viewbox 的内容。
        public System.Windows.Controls.StretchDirection StretchDirection { get; set; }

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

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

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

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

2.11.10 Adorner

表示修饰 UIElement 的 FrameworkElement 的抽象类。

装饰器是 FrameworkElement 绑定到的自定义 UIElement 。 装饰器呈现在装饰器层中,装饰器层是始终位于装饰元素或装饰元素集合之上的呈现图面;装饰器的呈现独立于呈现 UIElement 装饰器所绑定到的。 装饰器通常使用位于装饰元素左上部的标准 2D 坐标原点,相对于其绑定到的元素进行定位。

 备注

父级 Adorner 是 AdornerLayer 呈现的 Adorner ,而不是要装饰的元素。

 备注

放置在装饰器层中的任何内容将呈现在设置的其他任何样式的顶部。 换言之,装饰器始终以可见的方式位于顶部,无法使用 z 顺序重写。

    public abstract class Adorner : System.Windows.FrameworkElement
    {
        //获取此装饰器绑定到的 UIElement。
        public System.Windows.UIElement AdornedElement { get; }

        //获取或设置一个指示是否启用装饰器剪辑的值。
        public bool IsClipEnabled { get; set; }

        //基于当前应用于装饰元素的转换,返回装饰器的 Transform。
        public virtual System.Windows.Media.GeneralTransform GetDesiredTransform(System.Windows.Media.GeneralTransform transform);

        //有关此成员的说明,请参见 GetLayoutClip(Size)。
        protected override System.Windows.Media.Geometry GetLayoutClip(System.Windows.Size layoutSlotSize);

        //实现装饰器的任何自定义度量行为。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);
    }

AdornerDecorator

为可视化树中的子元素提供 AdornerLayer

AdornerDecorator 只能包含一个子元素。 该元素可以包含多个可以装饰的元素。

AdornerDecorator AdornerLayer 在可视化树中指定的位置。 它通常用于 ControlTemplate 可承载对象的控件 Adorner 。 例如,的 ControlTemplate Window 包含一个, AdornerDecorator 以便可以装饰窗口的子元素。 GetAdornerLayer null 如果传入元素,而该元素在 AdornerDecorator 其可视化树中不具有作为祖先的元素,则返回。

有关装饰器和可视化树的详细信息,请参阅WPF 中装饰器概述和树。

    public class AdornerDecorator : System.Windows.Controls.Decorator
    {

        //获取与此 AdornerLayer 关联的 AdornerDecorator。
        public System.Windows.Documents.AdornerLayer AdornerLayer { get; }

        //获取或设置 AdornerDecorator 的单个子元素。
        public override System.Windows.UIElement Child { get; set; }

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

        //定位子元素并确定 AdornerDecorator 的大小。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize);

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

        //测量子元素所需的大小,并确定 AdornerDecorator 的大小。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

    }

 

AdornerLayer

表示用于呈现装饰器的图面。

确保装饰器层的 Z 顺序大于) 正在装饰的元素 (,因此装饰器始终呈现在装饰的元素之上。

 备注

的父级 Adorner 是 AdornerLayer 呈现 装饰器 的,而不是要装饰的元素。

 备注

放置在装饰器层中的任何内容将呈现在设置的其他任何样式的顶部。 换言之,装饰器始终以可见的方式位于顶部,无法使用 z 顺序重写。

    public class AdornerLayer : System.Windows.FrameworkElement
    {

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

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

        //将装饰器添加到装饰器层。
        public void Add(System.Windows.Documents.Adorner adorner);

        //获取指定点的 AdornerHitTestResult。
        public System.Windows.Media.AdornerHitTestResult AdornerHitTest(System.Windows.Point point);

        //定位子元素并确定 AdornerLayer 的大小。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size finalSize);

        //返回可视化树中指定的 Visual 上方的第一个装饰器层。
        public static System.Windows.Documents.AdornerLayer GetAdornerLayer(System.Windows.Media.Visual visual);

        //返回装饰器的数组,这些装饰器绑定到指定的 UIElement 上。
        public System.Windows.Documents.Adorner[] GetAdorners(System.Windows.UIElement element);

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

        //测量子元素所需的大小,并确定 AdornerLayer 的大小。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //从装饰器层删除指定的 Adorner。
        public void Remove(System.Windows.Documents.Adorner adorner);

        //更新布局并重绘装饰器层中的所有装饰器。
        public void Update();
        public void Update(System.Windows.UIElement element);
        
    }

AnchoredBlock

为 Inline 元素提供基类的抽象类,这些元素用于将 Block 元素锚定到流内容。

    public abstract class AnchoredBlock : System.Windows.Documents.Inline
    {
        public static readonly System.Windows.DependencyProperty BorderBrushProperty;
        public static readonly System.Windows.DependencyProperty BorderThicknessProperty;
        public static readonly System.Windows.DependencyProperty LineHeightProperty;
        public static readonly System.Windows.DependencyProperty LineStackingStrategyProperty;
        public static readonly System.Windows.DependencyProperty MarginProperty;
        public static readonly System.Windows.DependencyProperty PaddingProperty;
        public static readonly System.Windows.DependencyProperty TextAlignmentProperty;

        //获取一个 BlockCollection,其中包含组成元素内容的顶级 Block 元素。
        public System.Windows.Documents.BlockCollection Blocks { get; }

        //获取或设置绘制元素边框时使用的 Brush。
        public System.Windows.Media.Brush BorderBrush { get; set; }

        //获取或设置元素的边框粗细。
        public System.Windows.Thickness BorderThickness { get; set; }

        //获取或设置各行内容的高度。
        public double LineHeight { get; set; }

        //获取或设置一种机制,根据该机制来为文本元素内的每一行文字确定一个行框。
        public System.Windows.LineStackingStrategy LineStackingStrategy { get; set; }

        //获取或设置元素的边距粗细。
        public System.Windows.Thickness Margin { get; set; }

        //获取或设置元素的填充厚度。
        public System.Windows.Thickness Padding { get; set; }

        //获取或设置一个值,该值指示文本内容的水平对齐方式。
        public System.Windows.TextAlignment TextAlignment { get; set; }

        //返回一个值,该值指示在序列化从 Blocks 派生的对象期间,是否应对 AnchoredBlock 属性的有效值进行序列化。
        public bool ShouldSerializeBlocks(System.Windows.Markup.XamlDesignerSerializationManager manager);
    }

参考微软MSDN

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值