XAML - 文本块



TextBlock 提供了一个轻量级的控件,用于显示少量只读文本。TextBlock 类的层次继承如下:

TextBlock Hierarchy

属性

序号 属性和描述
1

ContentEnd

获取 TextBlock 中文本内容末尾的 TextPointer 对象。

2

ContentStart

获取 TextBlock 中文本内容开头的 TextPointer 对象。

3

IsTextSelectionEnabled

获取或设置一个值,该值指示是否在 TextBlock 中启用文本选择,无论是通过用户操作还是调用与选择相关的 API。

4

IsTextSelectionEnabledProperty

标识 IsTextSelectionEnabled 依赖属性。

5

LineHeight

获取或设置每行内容的高度。

6

MaxLines

获取或设置 TextBlock 中显示的最大文本行数。

7

SelectedText

获取选定文本的文本范围。

8

SelectionEnd

获取 TextBlock 中选定文本的结束位置。

9

SelectionHighlightColor

获取或设置用于突出显示选定文本的画笔。

10

SelectionStart

获取 TextBlock 中选定文本的起始位置。

11

Text

获取或设置 TextBlock 的文本内容。

12

TextAlignment

获取或设置一个值,该值指示文本内容的水平对齐方式。

13

TextTrimming

获取或设置当内容超出内容区域时要使用的文本修剪行为。

14

TextWrapping

获取或设置 TextBlock 如何换行文本。

事件

序号 事件和描述
1

ContextMenuOpening

当系统处理显示上下文菜单的交互时发生。

2

SelectionChanged

当文本选择发生更改时发生。

方法

序号 方法和描述
1

Focus

使 TextBlock 获得焦点,就像它是一个常规的可获得焦点的控件一样。

2

Select

选择 TextBlock 中的一段文本。

3

SelectAll

选择 TextBlock 中的全部内容。

示例

以下示例演示了在 XAML 应用程序中使用 TextBlock 的方法。以下是创建和初始化具有某些属性的 TextBlock 的 XAML 代码。

<Window x:Class = "XAMLTextBlock.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   Title = "MainWindow" Height = "350" Width = "604">
	
   <Grid> 
      <StackPanel> 
         <TextBlock FontFamily = "Verdana" 
            LineStackingStrategy = "MaxHeight" 
            LineHeight = "10" Width = "500" TextWrapping = "Wrap">
				
            Use the <Run FontSize = "30"> LineStackingStrategy</Run> 
            property to determine how a line box is created for each line. 
            A value of <Run FontSize = "20">MaxHeight</Run> 
            specifies that the stack height is the smallest value 
            that contains all the inline elements on that line 
            when those elements are properly aligned. 
            A value of <Run FontSize = "20">BlockLineHeight</Run> 
            specifies that the stack height is determined by 
            the block element LineHeight property value.
         </TextBlock>
      </StackPanel> 
   </Grid>
   
</Window>

编译并执行上述代码后,将产生以下输出:

TextBlock Output

我们建议您执行上述示例代码,并尝试一些其他属性和事件。

xaml_controls.htm
广告
© . All rights reserved.