Xaml-richeditbox

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

XAML-RichEditBox

RichEditBoxは、書式設定されたテキスト、ハイパーリンク、およびその他のリッチコンテンツをサポートするリッチテキスト編集コントロールです。 WPFプロジェクトはこのコントロールをサポートしていません。 そのため、Windowsアプリに実装されます。 RichEditBoxクラスの階層的な継承は次のとおりです-

RichEditBox Hierarchy

プロパティ

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

Sr.No. Property & Description
1

AcceptsReturn

EnterキーまたはReturnキーが押されたときに、RichEditBoxが改行文字または改行文字を許可および表示するかどうかを示す値を取得または設定します。

2

AcceptsReturnProperty

AcceptsReturn依存プロパティを識別します。

3

DesiredCandidateWindowAlignment

Input Method Editor(IME)の優先配置を示す値を取得または設定します。

4

DesiredCandidateWindowAlignmentProperty

DesiredCandidateWindowAlignment依存プロパティを識別します。

5

Document

RichEditBoxに含まれるテキストのテキストオブジェクトモデルへのアクセスを可能にするオブジェクトを取得します。

6

Header

コントロールのヘッダーのコンテンツを取得または設定します。

7

HeaderProperty

Header依存プロパティを識別します。

8

HeaderTemplate

コントロールのヘッダーのコンテンツを表示するために使用されるDataTemplateを取得または設定します。

9

HeaderTemplateProperty

HeaderTemplate依存プロパティを識別します。

10

InputScope

このRichEditBoxによって使用される入力のコンテキストを取得または設定します。

11

InputScopeProperty

InputScope依存プロパティを識別します。

12

IsColorFontEnabled

Segoe UI Emojiなどのカラーレイヤーを含むフォントグリフをカラーでレンダリングするかどうかを決定する値を取得または設定します。

13

IsColorFontEnabledProperty

IsColorFontEnabled依存プロパティを識別します。

14

IsReadOnly

ユーザーがRichEditBoxのテキストを変更できるかどうかを示す値を取得または設定します。

15

IsReadOnlyProperty

IsReadOnly依存プロパティを識別します。

16

IsSpellCheckEnabled

テキスト入力がスペルチェックエンジンと対話する必要があるかどうかを示す値を取得または設定します。

17

IsSpellCheckEnabledProperty

IsSpellCheckEnabled依存プロパティを識別します。

18

IsTextPredictionEnabled

このRichEditBoxでテキスト予測機能(「オートコンプリート」)が有効かどうかを示す値を取得または設定します。

19

IsTextPredictionEnabledProperty

IsTextPredictionEnabled依存プロパティを識別します。

20

PlaceholderText

ユーザーアクションまたはその他の操作によって値が変更されるまで、コントロールに表示されるテキストを取得または設定します。

21

PlaceholderTextProperty

PlaceholderText依存プロパティを識別します。

22

PreventKeyboardDisplayOnProgrammaticFocus

コントロールがプログラムでフォーカスを受け取ったときに画面キーボードを表示するかどうかを示す値を取得または設定します。

23

PreventKeyboardDisplayOnProgrammaticFocusProperty

PreventKeyboardDisplayOnProgrammaticFocus依存プロパティを識別します。

24

SelectionHighlightColor

選択したテキストを強調表示するために使用されるブラシを取得または設定します。

25

SelectionHighlightColorProperty

SelectionHighlightColor依存プロパティを識別します。

26

TextAlignment

RichEditBoxでのテキストの配置方法を示す値を取得または設定します。

27

TextAlignmentProperty

TextAlignment依存プロパティを識別します。

28

TextReadingOrder

RichEditBoxの読み取り順序を決定する方法を示す値を取得または設定します。

29

TextReadingOrderProperty

TextReadingOrder依存プロパティを識別します。

30

TextWrapping

テキストの行がRichEditBoxの利用可能な幅を超えている場合、テキストの折り返しがどのように発生するかを示す値を取得または設定します。

31

TextWrappingProperty

TextWrapping依存プロパティを識別します。

イベント

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

Sr.No. Event & Description
1

CandidateWindowBoundsChanged

入力方式エディター(IME)ウィンドウが開いたとき、更新したとき、または閉じたときに発生します。

2

ContextMenuOpening

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

3

Paste

テキストがコントロールに貼り付けられたときに発生します。

4

SelectionChanged

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

5

TextChanged

RichEditBoxでコンテンツが変更されたときに発生します。

6

TextChanging

RichEditBoxのテキストが変更し始めたときに発生します。

7

TextCompositionChanged

入力方式エディター(IME)を介してテキストが作成されるときに発生します。

8

TextCompositionEnded

ユーザーがInput Method Editor(IME)によるテキストの作成を停止したときに発生します。

9

TextCompositionStarted

ユーザーがInput Method Editor(IME)を介してテキストの作成を開始すると発生します。

方法

以下は、RichEditBoxクラスで一般的に使用されるメソッドです。

Sr.No. Method & Description
1

OnManipulationCompleted

ManipulationCompletedイベントが発生する前に呼び出されます。 (Controlから継承)

2

OnManipulationDelta

ManipulationDeltaイベントが発生する前に呼び出されます。 (Controlから継承)

3

OnManipulationInertiaStarting

ManipulationInertiaStartingイベントが発生する前に呼び出されます。 (Controlから継承)

4

OnManipulationStarted

ManipulationStartedイベントが発生する前に呼び出されます。 (Controlから継承)

5

OnManipulationStarting

ManipulationStartingイベントが発生する前に呼び出されます。 (Controlから継承)

6

OnMaximumChanged

Maximumプロパティが変更されたときに呼び出されます。 (RangeBaseから継承)

7

OnMinimumChanged

Minimumプロパティが変更されたときに呼び出されます。 (RangeBaseから継承)

8

OnValueChanged

ValueChangedルーティングイベントを発生させます。 (RangeBaseから継承)

9

SetBinding

提供されたバインディングオブジェクトを使用して、FrameworkElementにバインディングをアタッチします。 (FrameworkElementから継承されます)

10

SetValue

DependencyObjectの依存関係プロパティのローカル値を設定します。 (DependencyObjectから継承)

11

StartDragAsync

ドラッグアンドドロップ操作を開始します。 (UIElementから継承)

12

UnregisterPropertyChangedCallback

RegisterPropertyChangedCallbackを呼び出して、以前に登録された変更通知をキャンセルします。 (DependencyObjectから継承)

次の例は、RichEditBoxでRTFファイルを開いて保存する方法を示しています。 2つのボタンと、いくつかのプロパティとイベントを含むRichEditBoxを作成および初期化するXAMLコードを次に示します。

<Page x:Class = "XAMLRichEditBox.MainPage"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local = "using:XAMLRichEditBox"
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
   mc:Ignorable = "d">

   <Grid Background = "{ThemeResource ApplicationPageBackgroundThemeBrush}">
      <Grid Margin = "120">
         <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
         </Grid.RowDefinitions>

         <StackPanel Orientation = "Horizontal">
            <Button Content = "Open file" Click = "OpenButton_Click"/>
            <Button Content = "Save file" Click = "SaveButton_Click"/>
         </StackPanel>

         <RichEditBox x:Name = "editor" Grid.Row = "1"/>
      </Grid>
   </Grid>

</Page>

さまざまなイベントとファイル処理のためのC#での実装は次のとおりです-

using System;
using System.Collections.Generic;
using System.IO; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;
using Windows.Foundation.Collections;

using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;

using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

//The Blank Page item template is documented at
http://go.microsoft.com/fwlink/?LinkId=234238

namespace XAMLRichEditBox {
  ///<summary>
     ///An empty page that can be used on its own or navigated to within a Frame.
  ///</summary>

   public sealed partial class MainPage : Page {
      public MainPage() {
         this.InitializeComponent();
      }
      private async void OpenButton_Click(object sender, RoutedEventArgs e) {
        //Open a text file.
         Windows.Storage.Pickers.FileOpenPicker open = new
            Windows.Storage.Pickers.FileOpenPicker();
         open.SuggestedStartLocation =
            Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
         open.FileTypeFilter.Add(".rtf");
         Windows.Storage.StorageFile file = await open.PickSingleFileAsync();

         if (file != null) {
            Windows.Storage.Streams.IRandomAccessStream randAccStream = await
               file.OpenAsync(Windows.Storage.FileAccessMode.Read);

           //Load the file into the Document property of the RichEditBox.
            editor.Document.LoadFromStream(Windows.UI.Text.TextSetOptions.FormatRtf, randAccStream);
         }
      }
      private async void SaveButton_Click(object sender, RoutedEventArgs e) {
         if (((ApplicationView.Value != ApplicationViewState.Snapped) ||
            ApplicationView.TryUnsnap())) {

            FileSavePicker savePicker = new FileSavePicker();
            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

           //Dropdown of file types the user can save the file as
            savePicker.FileTypeChoices.Add("Rich Text", new List <string>() { ".rtf" });

           //Default file name if the user does not type one in or select a file to replace
            savePicker.SuggestedFileName = "New Document";

            StorageFile file = await savePicker.PickSaveFileAsync();

            if (file != null) {
              //Prevent updates to the remote version of the file until we
              //finish making changes and call
               CompleteUpdatesAsync. CachedFileManager.DeferUpdates(file);

              //write to file
               Windows.Storage.Streams.IRandomAccessStream randAccStream = await
                  file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);

               editor.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf, randAccStream);

              //Let Windows know that we're finished changing the file so the
              //other app can update the remote version of the file.

               FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);

               if (status != FileUpdateStatus.Complete) {
                  Windows.UI.Popups.MessageDialog
                     errorBox = new Windows.UI.Popups.MessageDialog(
                     "File " + file.Name + " couldn't be saved.");

                  await errorBox.ShowAsync();
               }
            }
         }
      }
   }
}

上記のコードをコンパイルして実行すると、次の出力が生成されます。 このアプリケーションでRTFファイルを開いて、編集し、保存できます。

RichEditBox出力

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