Anvisningar: Använda stavningskontroll med en snabbmeny

När du aktiverar stavningskontroll i en redigeringskontroll som TextBox eller RichTextBoxvisas som standard alternativ för stavningskontroll i snabbmenyn. När användarna till exempel högerklickar på ett felstavat ord får de en uppsättning stavningsförslag eller alternativet att Ignorera alla. Men när du åsidosätter standardkontextmenyn med din egen anpassade snabbmeny går den här funktionen förlorad och du måste skriva kod för att återaktivera funktionen för stavningskontroll på snabbmenyn. I följande exempel visas hur du aktiverar detta på en TextBox.

Definiera en snabbmeny

I följande exempel visas XAML (Extensible Application Markup Language) som skapar en TextBox med vissa händelser som används för att implementera snabbmenyn.

<Page x:Class="SDKSample.SpellerCustomContextMenu"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Loaded="OnWindowLoaded">

  <TextBox
    Name="myTextBox" 
    TextWrapping="Wrap"
    SpellCheck.IsEnabled="True"
    ContextMenuOpening="tb_ContextMenuOpening">
    In a custum menu you need to write code to add speler choices
    because everything in a custom context menu has to be added explicitly.
  </TextBox>

</Page>

Implementera en snabbmeny

I följande exempel visas koden som implementerar snabbmenyn.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class SpellerCustomContextMenu : Page
    {

        void OnWindowLoaded(object sender, RoutedEventArgs e)
        {
            //This is required for the first time ContextMenu invocation so that TextEditor doesnt handle it.
            myTextBox.ContextMenu = GetContextMenu();
        }
        void tb_ContextMenuOpening(object sender, RoutedEventArgs e)
        {
            int caretIndex, cmdIndex;
            SpellingError spellingError;

            myTextBox.ContextMenu = GetContextMenu();
            caretIndex = myTextBox.CaretIndex;

            cmdIndex = 0;
            spellingError = myTextBox.GetSpellingError(caretIndex);
            if (spellingError != null)
            {
                foreach (string str in spellingError.Suggestions)
                {
                    MenuItem mi = new MenuItem();
                    mi.Header = str;
                    mi.FontWeight = FontWeights.Bold;
                    mi.Command = EditingCommands.CorrectSpellingError;
                    mi.CommandParameter = str;
                    mi.CommandTarget = myTextBox;
                    myTextBox.ContextMenu.Items.Insert(cmdIndex, mi);
                    cmdIndex++;
                }
                Separator separatorMenuItem1 = new Separator();
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem1);
                cmdIndex++;
                MenuItem ignoreAllMI = new MenuItem();
                ignoreAllMI.Header = "Ignore All";
                ignoreAllMI.Command = EditingCommands.IgnoreSpellingError;
                ignoreAllMI.CommandTarget = myTextBox;
                myTextBox.ContextMenu.Items.Insert(cmdIndex, ignoreAllMI);
                cmdIndex++;
                Separator separatorMenuItem2 = new Separator();
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem2);
            }
        }

        // Gets a fresh context menu.
        private ContextMenu GetContextMenu()
        {
            ContextMenu cm = new ContextMenu();

            //Can create STATIC custom menu items if exists here...
            MenuItem m1, m2, m3, m4;
            m1 = new MenuItem();
            m1.Header = "File";
            m2 = new MenuItem();
            m2.Header = "Save";
            m3 = new MenuItem();
            m3.Header = "SaveAs";
            m4 = new MenuItem();
            m4.Header = "Recent Files";

            //Can add functionality for the custom menu items here...

            cm.Items.Add(m1);
            cm.Items.Add(m2);
            cm.Items.Add(m3);
            cm.Items.Add(m4);

            return cm;
        }
    }
}

Namespace SDKSample
    Partial Public Class SpellerCustomContextMenu
        Inherits Page

        Private Sub OnWindowLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)

            'This is required for the first time ContextMenu invocation 
            'so that TextEditor doesnt handle it.
            myTextBox.ContextMenu = GetContextMenu()
        End Sub

        Private Sub tb_ContextMenuOpening(ByVal sender As Object,
                                          ByVal e As RoutedEventArgs)

            Dim caretIndex, cmdIndex As Integer
            Dim spellingError As SpellingError

            myTextBox.ContextMenu = GetContextMenu()
            caretIndex = myTextBox.CaretIndex

            cmdIndex = 0
            spellingError = myTextBox.GetSpellingError(caretIndex)
            If spellingError IsNot Nothing Then
                For Each str As String In spellingError.Suggestions
                    Dim mi As New MenuItem()
                    mi.Header = str
                    mi.FontWeight = FontWeights.Bold
                    mi.Command = EditingCommands.CorrectSpellingError
                    mi.CommandParameter = str
                    mi.CommandTarget = myTextBox
                    myTextBox.ContextMenu.Items.Insert(cmdIndex, mi)
                    cmdIndex += 1
                Next str
                Dim separatorMenuItem1 As New Separator()
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem1)
                cmdIndex += 1
                Dim ignoreAllMI As New MenuItem()
                ignoreAllMI.Header = "Ignore All"
                ignoreAllMI.Command = EditingCommands.IgnoreSpellingError
                ignoreAllMI.CommandTarget = myTextBox
                myTextBox.ContextMenu.Items.Insert(cmdIndex, ignoreAllMI)
                cmdIndex += 1
                Dim separatorMenuItem2 As New Separator()
                myTextBox.ContextMenu.Items.Insert(cmdIndex, separatorMenuItem2)
            End If
        End Sub

        ' Gets a fresh context menu. 
        Private Function GetContextMenu() As ContextMenu
            Dim cm As New ContextMenu()

            'Can create STATIC custom menu items if exists here...
            Dim m1, m2, m3, m4 As MenuItem
            m1 = New MenuItem()
            m1.Header = "File"
            m2 = New MenuItem()
            m2.Header = "Save"
            m3 = New MenuItem()
            m3.Header = "SaveAs"
            m4 = New MenuItem()
            m4.Header = "Recent Files"

            'Can add functionality for the custom menu items here...

            cm.Items.Add(m1)
            cm.Items.Add(m2)
            cm.Items.Add(m3)
            cm.Items.Add(m4)

            Return cm
        End Function

    End Class
End Namespace

Koden som används för att göra detta med en RichTextBox är liknande. Den största skillnaden finns i parametern som skickas till metoden GetSpellingError. För en TextBoxskickar du heltalsindexet för caret-positionen:

spellingError = myTextBox.GetSpellingError(caretIndex);

För en RichTextBoxskickar du den TextPointer som anger caret-positionen:

spellingError = myRichTextBox.GetSpellingError(myRichTextBox.CaretPosition);

Se även