Wpf-textblock

提供:Dev Guides
移動先:案内検索

WPF-テキストブロック

TextBlockは、少量の読み取り専用テキストを表示するための軽量なコントロールです。 TextBlockクラスの階層的な継承は次のとおりです-

テキストブロックの階層

TextBlockクラスの一般的に使用されるプロパティ

以下は、TextBlockの最も一般的に使用されるプロパティです。

Sr. No. Property & Description
1

ContentEnd

TextBlockのテキストコンテンツの終わりのTextPointerオブジェクトを取得します。

2

ContentStart

TextBlockのテキストコンテンツの開始点のTextPointerオブジェクトを取得します。

3

IsTextSelectionEnabled

ユーザーアクションまたは選択関連APIの呼び出しによって、TextBlockでテキスト選択が有効になっているかどうかを示す値を取得または設定します。

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によるテキストのラップ方法を取得または設定します。

TextBlockクラスの一般的に使用されるイベント

以下は、TextBlockの最も一般的に使用されるイベントです。

Sr. No. Event & Description
1

ContextMenuOpening

システムがコンテキストメニューを表示するインタラクションを処理するときに発生します。

2

SelectionChanged

テキストの選択が変更されたときに発生します。

TextBlockクラスで一般的に使用されるメソッド

以下に、TextBlockの最も一般的に使用されるメソッドを示します。

Sr. No. Method & Description
1

Focus

TextBlockを、従来のフォーカス可能なコントロールであるかのようにフォーカスします。

2

Select

TextBlock内のテキストの範囲を選択します。

3

SelectAll

TextBlockのコンテンツ全体を選択します。

  • WPFTextBlockControlを使用して新しいWPFプロジェクトを作成しましょう。
  • ツールボックスからテキストブロックをドラッグします。
  • プロパティウィンドウからテキストブロックの背景色を変更します。
  • 次の例は、XAMLアプリケーションでのTextBlockの使用方法を示しています。
  • 以下に、いくつかのプロパティを使用してTextBlockが作成されるXAMLコードを示します。
<Window x:Class = "WPFTextBlockControl.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:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:local = "clr-namespace:WPFTextBlockControl"
   mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">

   <Grid>
      <TextBlock FontFamily = "Verdana"
         LineStackingStrategy = "MaxHeight" LineHeight = "10" Width = "500"
         TextWrapping = "Wrap" Background = "#FFE2B1B1" Margin = "48,8,48,10">

         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>
   </Grid>

</Window>

上記のコードをコンパイルして実行すると、次の出力が生成されます-

テキストブロックの出力

上記のサンプルコードを実行し、TextBlockクラスの他のプロパティとイベントを試すことをお勧めします。