How to show suggestions for spelling mistakes using context menu?
The SfRichTextBoxAdv control provides API to check the spelling mistake of current word in selection and get the list of suggestions for the misspelled words (if any). Using this option, you can easily customize the spell-checking functionality in context menu.
The following sample code demonstrates how to initialize SfRichTextBoxAdv control and context menu for it.
XAML
<RichTextBoxAdv:SfRichTextBoxAdv x:Name="richTextBoxAdv" ManipulationMode="All" Grid.Row="1" xmlns:RichTextBoxAdv="using:Syncfusion.UI.Xaml.RichTextBoxAdv"/>
The following sample code demonstrates declaring a variable for holding the context menu instance for SfRichTextBoxAdv control.
C#
// Represents the context menu for SfRichTextBoxAdv MenuFlyout flyout = null;
The following sample code demonstrates enabling the spell checker for SfRichTextBoxAdv control, initializing the context menu and adding event handlers for enabling context menu on right tap.
C#
// Enables the spell checker. richTextBoxAdv.SpellChecker.IsEnabled = true; ' // Adds the language dictionary to spellchecker. richTextBoxAdv.SpellChecker.Dictionaries.Add("ms-appx:///Assets/en_US.dic"); // Initializes the context menu flyout = new MenuFlyout(); FlyoutBase.SetAttachedFlyout(richTextBoxAdv, flyout); // Adds event handler for right tapped to enable context menu for spell-checking. richTextBoxAdv.RightTapped += RichTextBoxAdv_RightTapped; // Adds event handler for selection changed and flyout closed to enable radial menu after context menu closed. richTextBoxAdv.SelectionChanged += RichTextBoxAdv_SelectionChanged; flyout.Closed += Flyout_Closed;
The following sample code demonstrates the event handlers to enable context menu for spell-checking.
C#
// Called when right tapped in SfRichTextBoxAdv. private void RichTextBoxAdv_RightTapped(object sender, RightTappedRoutedEventArgs e) { CheckSpellingUsingContextMenu(e.GetPosition(richTextBoxAdv)); } // Check spelling using context menu. public void CheckSpellingUsingContextMenu(Point point) { // Clears previous items in the context menu. flyout.Items.Clear(); // Gets the cursor position. TextPosition start = richTextBoxAdv.Selection.Start; // Checks the spelling mistake of current word. SpellingError error = richTextBoxAdv.GetSpellingError(start); // If there is spelling mistake, shows context menu with suggestions(if any). if (error != null) { // Disables the built-in radial menu. richTextBoxAdv.EnableRadialMenu = false; // Gets the suggestions for spelling mistakes. List<string> suggestions = error.Suggestions.ToList<string>(); if (suggestions.Count > 0) { for (int i = 0; i < suggestions.Count && i < 5; i++) { // Adds the suggested to the context menu. MenuFlyoutItem item = new MenuFlyoutItem(); item.Text = suggestions[i]; flyout.Items.Add(item); // Sets the change spelling command with suggested text as parameter. item.Command = richTextBoxAdv.ChangeSpellingCommand; item.CommandParameter = item.Text; } } else flyout.Items.Add(new MenuFlyoutItem() { Text = "No suggestions", IsEnabled = false }); // Shows the context menu. flyout.ShowAt(richTextBoxAdv, point); } else { // Enables the built-in radial menu. richTextBoxAdv.EnableRadialMenu = true; richTextBoxAdv.Focus(FocusState.Pointer); } } // Called when selection is changed. private void RichTextBoxAdv_SelectionChanged(object obj, Syncfusion.UI.Xaml.RichTextBoxAdv.SelectionChangedEventArgs args) { // Enables the built-in radial menu. richTextBoxAdv.EnableRadialMenu = true; } // Called when the context menu is closed private void Flyout_Closed(object sender, object e) { // Enables the built-in radial menu on context menu closed. richTextBoxAdv.EnableRadialMenu = true; richTextBoxAdv.Focus(FocusState.Pointer); }
The SfRichTextBoxAdv control provides API for retrieving the bounds of the cursor position. Using this option, you can easily customize enabling context menu through code behind.
The following sample code demonstrates how to enable context menu through code behind.
C#
// Gets the selection start text position. TextPosition start = richTextBoxAdv.Selection.Start; // Gets the bounds of the text position. Rect rect = start.GetRect(); // Shows context menu at the cursor position. CheckSpellingUsingContextMenu(new Point(rect.X, rect.Y));
You can find the sample from following link.