UI⑥FrameworkElement族控件TextBlock

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


TextBlock

提供一个轻型控件,用于显示少量流内容。

Remarks

TextBlock可以在其 Text 属性或 Inline 流内容元素中包含字符串,如在Inlines属性内的 Bold 、 Hyperlink 和 InlineUIContainer

TextBlock设计为轻巧的,专门用于将流内容的一小部分集成到用户界面(UI)中。 TextBlock针对单行显示进行了优化,并为显示多达几行的内容提供了良好的性能。

对于需要显示多行内容的场景,TextBlock并未进行优化。在这种情况下,就性能而言,将FlowDocument与适当的查看控件结合使用是比TextBlock更好的选择。在TextBlock之后,FlowDocumentScrollViewer是显示流内容的下一个最轻量的控件,它仅提供具有最少UI的滚动内容区域。 FlowDocumentPageViewer针对流内容的“一次页面”查看模式进行了优化。最后,FlowDocumentReader支持用于查看流内容的最丰富的设置功能,但重量相应地更大。

使用TextAlignment属性可以在TextBlock中水平对齐文本。使用Horizo​​ntalAlignmentVerticalAlignment属性在页面布局内对齐TextBlock

字段
名称备注权限
BackgroundProperty标识 Background 依赖项属性。public static readonly
BaselineOffsetProperty标识 BaselineOffset 依赖项属性。public static readonly
FontFamilyProperty标识 FontFamily 依赖项属性。public static readonly
FontSizeProperty标识 FontSize 依赖项属性。public static readonly
FontStretchProperty标识 FontStretch 依赖项属性。public static readonly
FontStyleProperty标识 FontStyle 依赖项属性。public static readonly
FontWeightProperty标识 FontWeight 依赖项属性。public static readonly
ForegroundProperty标识 Foreground 依赖项属性。public static readonly
IsHyphenationEnabledProperty标识 IsHyphenationEnabled 依赖项属性。public static readonly
LineHeightProperty标识 LineHeight 依赖项属性。public static readonly
LineStackingStrategyProperty标识 LineStackingStrategy 依赖项属性。public static readonly
PaddingProperty标识 Padding 依赖项属性。public static readonly
TextAlignmentProperty标识 TextAlignment 依赖项属性。public static readonly
TextDecorationsProperty标识 TextDecorations 依赖项属性。public static readonly
TextEffectsProperty标识 TextEffects 依赖项属性。public static readonly
TextProperty标识 Text 依赖项属性。public static readonly
TextTrimmingProperty标识 TextTrimming 依赖项属性。public static readonly
TextWrappingProperty标识 TextWrapping 依赖项属性。public static readonly
属性
名称备注权限
Background获取或设置要用于填充内容区域背景的 Brushget; set;
BaselineOffset获取或设置文本的每个行相对于基线的偏移量。get; set;
BreakAfter获取一个 LineBreakCondition,它指示内容应当如何在当前元素之后进行分行。get;
BreakBefore获取一个 LineBreakCondition,它指示内容应当如何在当前元素之前进行分行。get;
ContentEnd获取一个指向 TextPointer 中内容的末尾的 TextBlockget;
ContentStart获取一个指向 TextPointer 中内容的开始位置的 TextBlockget;
FontFamily获取或设置 TextBlock 的首选最常用字体系列。get; set;
FontSize获取或设置 TextBlock 的顶级字号。get; set;
FontStretch获取或设置 TextBlock 的常用字体拉伸特征。get; set;
FontStyle获取或设置 TextBlock 的顶级字体样式。get; set;
FontWeight获取或设置 TextBlock 的顶级字体粗细。get; set;
Foreground获取或设置要应用到 TextBlock 的文本内容的 Brushget; set;
HostedElementsCore获取一个可用于迭代此 TextBlock 承载的元素的枚举数。get;
Inlines获取包含顶级 Inline 元素的 InlineCollection,前者构成 TextBlock 的内容。get;
IsHyphenationEnabled获取或设置一个值,该值指示是否启用文字的自动断字功能。get; set;
LineHeight获取或设置各行内容的高度。get; set;
LineStackingStrategy获取或设置用于为 TextBlock 中的每行文本确定行框的机制。get; set;
LogicalChildren获取一个可循环访问 TextBlock 的逻辑子级的枚举器。get;
Padding获取或设置一个值,该值指示内容区域的边界之间填充空间的宽度,以及由 TextBlock 显示的内容。get; set;
Text获取或设置 TextBlock 的文本内容。get; set;
TextAlignment获取或设置一个值,该值指示文本内容的水平对齐方式。get; set;
TextDecorations获取或设置 TextDecorationCollection,它包含应用于 TextBlock 的文本的效果。get; set;
TextEffects获取或设置要应用于此元素中的文本内容的效果。get; set;
TextTrimming获取或设置在内容超出内容区域时要采用的文本剪裁行为。get; set;
TextWrapping获取或设置 TextBlock 对文本进行换行的方式。get; set;
Typography获取此元素的内容的当前有效版式变体。get;
VisualChildrenCount获取 Visual 所承载的 TextBlock 子项的数目。get;
方法
名称备注权限
ArrangeOverride定位子元素并确定 TextBlock 的大小。protected
GetBaselineOffset返回指定的依赖对象的 BaselineOffset 附加属性值。public
GetFontFamily返回指定的依赖对象的 FontFamilyProperty 附加属性值。public
GetFontSize返回指定的依赖对象的 FontSize 附加属性值。public
GetFontStretch返回指定的依赖对象的 FontStretch 附加属性值。public
GetFontStyle返回指定的依赖对象的 FontStyle 附加属性值。public
GetFontWeight返回指定的依赖对象的 FontWeight 附加属性值。public
GetForeground返回指定的依赖对象的 Foreground 附加属性值。public
GetLineHeight返回指定的依赖对象的 LineHeight 附加属性值。public
GetLineStackingStrategy

返回指定的依赖对象的 LineStackingStrategy 附加属性值。

public
GetPositionFromPoint返回指向最靠近指定 TextPointer 的位置的 Pointpublic
GetRectanglesCore返回指定的 ContentElement 的边框的只读集合。protected
GetTextAlignment返回指定的依赖对象的 TextAlignment 附加属性值。public
GetVisualChild返回指定索引位置处的 Visual 子项。protected
HitTestCore返回指定 PointHitTestResult 的 PointHitTestParametersprotected
InputHitTestCore返回 IInputElement 内指定 Point 处的 TextBlockprotected
MeasureOverride调用以重新测量 TextBlockprotected
OnChildDesiredSizeChangedCore当派生自 UIElement 的子元素更改其 DesiredSize 时调用。protected
OnCreateAutomationPeer创建并返回此 AutomationPeer 的 TextBlock 对象。protected
OnPropertyChanged当一个或多个托管依赖属性的值发生更改时调用。protected
OnRender呈现 TextBlock 的内容。protected
SetBaselineOffset设置指定的依赖对象上的 BaselineOffset 附加属性值。public
SetFontFamily设置指定的依赖对象上的 FontFamily 附加属性值。public
SetFontSize设置指定的依赖对象上的 FontSize 附加属性值。public
SetFontStretch设置指定的依赖对象上的 FontStretch 附加属性值。public
SetFontStyle设置指定的依赖对象上的 FontStyle 附加属性值。public
SetFontWeight设置指定的依赖对象上的 FontWeight 附加属性值。public
SetForeground设置指定的依赖对象上的 Foreground 附加属性值。public
SetLineHeight设置指定的依赖对象上的 LineHeight 附加属性值。public
SetLineStackingStrategy设置指定的依赖对象上的 LineStackingStrategy 附加属性值。public
SetTextAlignment设置指定的依赖对象上的 TextAlignment 附加属性值。public
ShouldSerializeBaselineOffset返回一个值,该值指示在序列化 BaselineOffset 对象期间,是否应对 TextBlock 属性的有效值进行序列化。public
ShouldSerializeInlines返回一个值,该值指示在序列化 Inlines 对象期间,是否应对 TextBlock 属性的有效值进行序列化。public
ShouldSerializeText返回一个值,该值指示在序列化 Text 对象期间,是否应对 TextBlock 属性的有效值进行序列化。public
接口
名称备注
IServiceProvider.GetServiceobject IServiceProvider.GetService (Type serviceType);
IContentHost.GetRectanglesSystem.Collections.ObjectModel.ReadOnlyCollection<System.Windows.Rect> IContentHost.GetRectangles (System.Windows.ContentElement child);
IContentHost.HostedElementsSystem.Collections.Generic.IEnumerator<System.Windows.IInputElement> System.Windows.IContentHost.HostedElements { get; }
IContentHost.InputHitTestSystem.Windows.IInputElement IContentHost.InputHitTest (System.Windows.Point point);
IContentHost.OnChildDesiredSizeChangedvoid IContentHost.OnChildDesiredSizeChanged (System.Windows.UIElement child);
IAddChild.AddChildvoid IAddChild.AddChild (object value);
IAddChild.AddTextvoid IAddChild.AddText (string text);

 

 

 

 

 

 

 

 

 



XAML范例

TextTrimming="WordEllipsis"使不能完整显示的englishword用...代替

<Window
    x:Class="TextBlockDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:TextBlockDemo"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="300"
    Height="250"
    mc:Ignorable="d">
    <StackPanel>
        <TextBlock
            Background="LightGoldenrodYellow"
            FontFamily="楷体"
            FontSize="24"
            FontStretch="UltraExpanded"
            FontStyle="Italic"
            FontWeight="UltraBold"
            Text="中国人用中国字写englishword"
            TextAlignment="Center"
            TextDecorations="Strikethrough"
            TextTrimming="WordEllipsis"
            TextWrapping="NoWrap" />
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TextBlockDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

C#范例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TextBlockDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            TextBlock textBlock = new TextBlock { Background = Brushes.LightGoldenrodYellow,
                FontFamily = new FontFamily("楷体"),
                FontSize = 24,
                FontStretch= FontStretches.UltraExpanded,
                FontStyle= FontStyles.Italic,
                FontWeight= FontWeights.UltraBold,
                Text = "中国人用中国字写englishword",
                TextAlignment= TextAlignment.Center,
                TextDecorations= TextDecorations.Strikethrough,
                TextTrimming= TextTrimming.WordEllipsis,
                TextWrapping= TextWrapping.NoWrap
            };

            StackPanel stackPanel = new StackPanel();
            stackPanel.Children.Add(textBlock);
            this.Content = stackPanel;
        }
    }
}

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值