WPF基础四:UI的相关类 (8)FrameworkElement的其他派生类

本文档详细介绍了WPF中的多种控件,如AccessText、AdornedElementPlaceholder、ColumnDefinition、ContentPresenter、ControlTemplate、Image、InkCanvas、ItemsPanelTemplate、ItemsPresenter、MediaElement、Page、RowDefinition、ScrollContentPresenter、TextBlock 和 Viewport3D。这些控件在构建用户界面时扮演着不同的角色,涉及布局、媒体播放、文本显示和自定义模板等功能。

目录

2.8 其他FrameworkElement的派生

2.8.1 AccessText 

2.8.2 AdornedElementPlaceholder

2.8.3 ColumnDefinition

2.8.4 ContentPresenter

2.8.5 ControlTemplate

2.8.6 Image

2.8.7 InkCanvas

2.8.8 ItemsPanelTemplate

2.8.9 ItemsPresenter

2.8.10 MediaElement

2.8.11 Page

2.8.12 RowDefinition

2.8.13 ScrollContentPresenter

2.8.14 TextBlock

2.8.15 Viewport3D

2.8.16 WebBrowser


2.8 其他FrameworkElement的派生


2.8.1 AccessText 

用下划线来指定用作访问键的字符。

如果内容包含多个下划线字符,则只会将第一个下划线字符转换为, AccessKey 其他下划线将显示为普通文本。 如果要转换为访问键的下划线不是第一个下划线,请在要转换的下划线前面使用两个连续的下划线。 例如,以下代码包含一个访问键,并显示为 _Hello W orld:

<AccessText>__Hello_World</AccessText>   

由于 H 前面的下划线为 double,因此 W key 注册为访问键。

若要 AccessText 在控件样式中用作内容宿主,请 RecognizesAccessKey 在中设置 ContentPresenter ,如以下示例所示:

<ContentPresenter RecognizesAccessKey="True"/>   
    public class AccessText : FrameworkElement, IAddChild
    {
        public static readonly System.Windows.DependencyProperty BackgroundProperty;
        public static readonly System.Windows.DependencyProperty BaselineOffsetProperty;
        public static readonly System.Windows.DependencyProperty FontFamilyProperty;
        public static readonly System.Windows.DependencyProperty FontSizeProperty;
        public static readonly System.Windows.DependencyProperty FontStretchProperty;
        public static readonly System.Windows.DependencyProperty FontStyleProperty;
        public static readonly System.Windows.DependencyProperty FontWeightProperty;
        public static readonly System.Windows.DependencyProperty ForegroundProperty;
        public static readonly System.Windows.DependencyProperty LineHeightProperty;
        public static readonly System.Windows.DependencyProperty LineStackingStrategyProperty;
        public static readonly System.Windows.DependencyProperty TextAlignmentProperty;
        public static readonly System.Windows.DependencyProperty TextDecorationsProperty;
        public static readonly System.Windows.DependencyProperty TextEffectsProperty;
        public static readonly System.Windows.DependencyProperty TextProperty;
        public static readonly System.Windows.DependencyProperty TextTrimmingProperty;
        public static readonly System.Windows.DependencyProperty TextWrappingProperty;

        //用于以只读方式访问第一个下划线字符后面的字符。
        public char AccessKey { get; }

        //获取或设置填充内容区域的 Brush。
        public System.Windows.Media.Brush Background { get; set; }

        //获取或设置一个值,该值调整文本在 AccessText 元素中的基线偏移位置。
        public double BaselineOffset { get; set; }

        //获取或设置要用于 AccessText 元素的字体系列。
        public System.Windows.Media.FontFamily FontFamily { get; set; }

        //获取或设置要用于 AccessText 元素的字体大小。
        public double FontSize { get; set; }

        //获取或设置一个 FontStretch 属性,该属性从 FontFamily 中选择一个正常、压缩或扩展的字体。
        public System.Windows.FontStretch FontStretch { get; set; }

        //获取或设置要用于 AccessText 元素的字体样式。
        public System.Windows.FontStyle FontStyle { get; set; }

        //获取或设置要用于 AccessText 元素的字体粗细。
        public System.Windows.FontWeight FontWeight { get; set; }

        //获取或设置用于绘制元素的文本内容的 Brush。
        public System.Windows.Media.Brush Foreground { get; set; }

        //获取或设置每个行框的高度。
        public double LineHeight { get; set; }

        //获取或设置如何强制应用 LineHeight 属性。
        public System.Windows.LineStackingStrategy LineStackingStrategy { get; set; }

        //获取一个循环访问该 AccessText 元素的逻辑子元素的枚举数。
        protected internal override System.Collections.IEnumerator LogicalChildren { get; }

        //获取或设置 AccessText 元素所显示的文本。
        public string Text { get; set; }

        //获取或设置内容的水平对齐方式。
        public System.Windows.TextAlignment TextAlignment { get; set; }

        //获取或设置添加到 AccessText 元素的文本的修饰。
        public System.Windows.TextDecorationCollection TextDecorations { get; set; }

        //获取或设置添加到 AccessText 元素的文本的效果。
        public System.Windows.Media.TextEffectCollection TextEffects { get; set; }

        //获取或设置在 AccessText 元素的文本内容溢出行框时应如何裁剪这些内容。
        public System.Windows.TextTrimming TextTrimming { get; set; }

        //获取或设置当 AccessText 元素的文本内容溢出行框时是否换行。
        public System.Windows.TextWrapping TextWrapping { get; set; }

        //获取可见子元素的数量。
        protected override int VisualChildrenCount { get; }

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

        //获取可见子元素的索引。
        protected override System.Windows.Media.Visual GetVisualChild(int index);

        //重新测量控件。
        protected override sealed System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //有关此成员的说明,请参见 AddChild(Object)。
        void IAddChild.AddChild(object value);

        //有关此成员的说明,请参见 AddText(String)。
        void IAddChild.AddText(string text);
    }

2.8.2 AdornedElementPlaceholder

表示 ControlTemplate 中使用的元素,该元素用于指定修饰控件相对于 ControlTemplate 中的其他元素所放置的位置。

仅当你要创建一个用作 ControlTemplate 自定义验证的, ErrorTemplate 以便在用户输入无效时提供可视反馈时,才使用此类。

数据绑定验证错误

例如,当输入无效时,您可能希望在文本框旁显示一个红色感叹号,如下图所示。数据绑定验证错误

 

XAML 对象元素用法

<AdornedElementPlaceholder/>  
    public class AdornedElementPlaceholder : FrameworkElement, IAddChild
    {
        //获取此 UIElement 对象为其保留空间的 AdornedElementPlaceholder。
        public System.Windows.UIElement AdornedElement { get; }

        //获取或设置此 AdornedElementPlaceholder 对象的单一子对象。
        public virtual System.Windows.UIElement Child { get; set; }

        //获取 AdornedElementPlaceholder 对象的逻辑子元素的枚举器。
        protected internal override System.Collections.IEnumerator LogicalChildren { get; }

        //获取可见子对象的数目。
        protected override int VisualChildrenCount { get; }

        //定位第一个可见子对象并返回此 AdornedElementPlaceholder 对象所要求的布局大小。
        protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeBounds);

        //检索位于指定索引处的 Visual 子对象。
        protected override System.Windows.Media.Visual GetVisualChild(int index);

        //确定 AdornedElementPlaceholder 对象的大小。
        protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint);

        //引发 IsInitialized 事件。 当在内部将 IsInitialized 设置为 true 时,将调用此方法。
        protected override void OnInitialized(EventArgs e);

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

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

2.8.3 ColumnDefinition

定义将应用于 Grid 元素的特定于列的属性。

ColumnDefinition 使用其自己的特殊结构 a GridLength 来描述其宽度特征。 GridLength 允许 ColumnDefinition 支持可用空间的变量分布。

    public class ColumnDefinition : DefinitionBase
    {
        public static readonly System.Windows.DependencyProperty MaxWidthProperty;
        public static readonly System.Windows.DependencyProperty MinWidthProperty;
        public static readonly System.Windows.DependencyProperty WidthProperty;

        //获取一个值,该值表示实际计算所得的 ColumnDefinition 宽度。
        public double ActualWidth { get; }

        //获取或设置一个值,该值表示 ColumnDefinition 的最大宽度。
        public double MaxWidth { get; set; }

        //获取或设置一个值,该值表示 ColumnDefinition 的最小宽度。
        public double MinWidth { get; set; }

        //获取一个值,该值表示此 ColumnDefinition 的偏移量值。
        public double Offset { get; }

        //获取 ColumnDefinition 元素的计算所得宽度,或设置由 ColumnDefinition 定义的列的 GridLength 值。
        public System.Windows.GridLength Width { get; set; }

    }


    public abstract class DefinitionBase : FrameworkContentElement
    {
        public static readonly System.Windows.DependencyProperty SharedSizeGroupProperty;

        //获取或设置一个值,该值将 ColumnDefinition 或 RowDefinition 标识为已定义的共享调整大小属性的组的成员。
        public string SharedSizeGroup { get; set; }
    }

2.8.4 ContentPresenter

显示 ContentControl 的内容。

通常使用的 ContentPresenter 中的 ControlTemplate ContentControl 来指定要添加内容的位置。 每个 ContentControl 类型的 ContentPresenter 默认值均为 ControlTemplate 。

当 ContentPresenter 对象位于的中时, ControlTemplate ContentControl Content 、 ContentTemplate 和属性将 ContentTemplateSelector 从相同名称的属性中获取其值 ContentControl 。 可以让 ContentPresenter 属性通过设置 ContentSource 属性或绑定到模板父级的其他属性来获取这些属性的值。

ContentPresenter使用以下逻辑显示 Content :

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值