XAML - RichEditBox

A RichEditBox is a rich text editing control that supports formatted text, hyperlinks, and other rich content. WPF projects do not support this control. So it will be implemented in Windows App. The hierarchical inheritance of RichEditBox class is as follows −
AcceptsReturn
Gets or sets a value that indicates whether the RichEditBox allows and displays the newline or return characters when the ENTER or RETURN keys are pressed.
AcceptsReturnProperty
Identifies the AcceptsReturn dependency property.
DesiredCandidateWindowAlignment
Gets or sets a value that indicates the preferred alignment of the Input Method Editor (IME).
DesiredCandidateWindowAlignmentProperty
Identifies the DesiredCandidateWindowAlignment dependency property.
Document
Gets an object that enables access to the text object model for the text contained in a RichEditBox.
Header
Gets or sets the content for the control's header.
HeaderProperty
Identifies the Header dependency property.
HeaderTemplate
Gets or sets the DataTemplate used to display the content of the control's header.
HeaderTemplateProperty
Identifies the HeaderTemplate dependency property.
InputScope
Gets or sets the context for input used by this RichEditBox.
InputScopeProperty
Identifies the InputScope dependency property.
IsColorFontEnabled
Gets or sets a value that determines whether font glyphs that contain color layers, such as Segoe UI Emoji, are rendered in color.
IsColorFontEnabledProperty
Identifies the IsColorFontEnabled dependency property.
IsReadOnly
Gets or sets a value that indicates whether the user can change the text in the RichEditBox.
IsReadOnlyProperty
Identifies the IsReadOnly dependency property.
IsSpellCheckEnabled
Gets or sets a value that indicates whether the text input should interact with a spell check engine.
IsSpellCheckEnabledProperty
Identifies the IsSpellCheckEnabled dependency property.
IsTextPredictionEnabled
Gets or sets a value that indicates whether text prediction features ("autocomplete") are enabled for this RichEditBox.
IsTextPredictionEnabledProperty
Identifies the IsTextPredictionEnabled dependency property.
PlaceholderText
Gets or sets the text that is displayed in the control until the value is changed by a user action or some other operation.
PlaceholderTextProperty
Identifies the PlaceholderText dependency property.
PreventKeyboardDisplayOnProgrammaticFocus
Gets or sets a value that indicates whether the on-screen keyboard is shown when the control receives focus programmatically.
PreventKeyboardDisplayOnProgrammaticFocusProperty
Identifies the PreventKeyboardDisplayOnProgrammaticFocus dependency property.
SelectionHighlightColor
Gets or sets the brush used to highlight the selected text.
SelectionHighlightColorProperty
Identifies the SelectionHighlightColor dependency property.
TextAlignment
Gets or sets a value that indicates how text is aligned in the RichEditBox.
TextAlignmentProperty
Identifies the TextAlignment dependency property.
TextReadingOrder
Gets or sets a value that indicates how the reading order is determined for the RichEditBox.
TextReadingOrderProperty
Identifies the TextReadingOrder dependency property.
TextWrapping
Gets or sets a value that indicates how text wrapping occurs if a line of text extends beyond the available width of the RichEditBox.
TextWrappingProperty
Identifies the TextWrapping dependency property.
CandidateWindowBoundsChanged
Occurs when the Input Method Editor (IME) window open, updates, or closes.
ContextMenuOpening
Occurs when the system processes an interaction that displays a context menu.
Paste
Occurs when text is pasted into the control.
SelectionChanged
Occurs when the text selection has changed.
TextChanged
Occurs when content changes in the RichEditBox.
TextChanging
Occurs when the text in the RichEditBox starts to change.
TextCompositionChanged
Occurs when text being composed through an Input Method Editor (IME) changes.
TextCompositionEnded
Occurs when a user stops composing text through an Input Method Editor (IME).
TextCompositionStarted
Occurs when a user starts composing text through an Input Method Editor (IME).
OnManipulationCompleted
Called before the ManipulationCompleted event occurs. (Inherited from Control)
OnManipulationDelta
Called before the ManipulationDelta event occurs. (Inherited from Control)
OnManipulationInertiaStarting
Called before the ManipulationInertiaStarting event occurs. (Inherited from Control)
OnManipulationStarted
Called before the ManipulationStarted event occurs. (Inherited from Control)
OnManipulationStarting
Called before the ManipulationStarting event occurs. (Inherited from Control)
OnMaximumChanged
Called when the Maximum property changes. (Inherited from RangeBase)
OnMinimumChanged
Called when the Minimum property changes. (Inherited from RangeBase)
OnValueChanged
Fires the ValueChanged routed event. (Inherited from RangeBase)
SetBinding
Attaches a binding to a FrameworkElement, using the provided binding object. (Inherited from FrameworkElement)
SetValue
Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject)
StartDragAsync
Initiates a drag-and-drop operation. (Inherited from UIElement)
UnregisterPropertyChangedCallback
Cancels a change notification that was previously registered by calling RegisterPropertyChangedCallback. (Inherited from DependencyObject)
The following example shows how to open and save an RTF file in RichEditBox. Here is the XAML code to create and initialize two buttons and a RichEditBox with some properties and events.
<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>
Here is the implementation in C# for different events and file handling −
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(); 
               } 
            }
         } 
      } 
   } 
}
When you compile and execute the above code, it will produce the following output. You can open, edit, and save any RTF file in this application.
We recommend you to execute the above example code and experiment with some other properties and events.
 Print
 Add Notes
 Bookmark this page