自定义词典的Wpf文本框拼写检查问题

自定义词典的Wpf文本框拼写检查问题,wpf,textbox,spell-checking,Wpf,Textbox,Spell Checking,我在Wpf应用程序中遇到了自定义词典的拼写检查问题,因为它无法识别我∆n 如果我添加一个行为来尝试和处理我∆n、 DoSpellCheck()在您将文本键入或粘贴到文本框中时起作用,但在调用Textbox_Loaded(object sender,RoutedEventArgs e)时不起作用 任何帮助都将不胜感激 公共类TextBoxextendedPellCheckBehavior:Behavior { 私有静态密钥[]_nonInputKeys=新密钥[]{Key.Left,Key.Up,

我在Wpf应用程序中遇到了自定义词典的拼写检查问题,因为它无法识别我∆n

如果我添加一个行为来尝试和处理我∆n、 DoSpellCheck()在您将文本键入或粘贴到文本框中时起作用,但在调用Textbox_Loaded(object sender,RoutedEventArgs e)时不起作用

任何帮助都将不胜感激

公共类TextBoxextendedPellCheckBehavior:Behavior { 私有静态密钥[]_nonInputKeys=新密钥[]{Key.Left,Key.Up,Key.Right,Key.Down,Key.Tab,Key.Delete,Key.Back,Key.Escape,Key.Enter}

    protected override void OnAttached()
    {
        if (AssociatedObject is TextBox)
        {
            TextBox textBox = AssociatedObject as TextBox;
            textBox.PreviewKeyUp += OnTextBox_PreviewKeyUp;
            textBox.LostFocus += OnTextBox_LostFocus;
            textBox.TextChanged += OnTextChanged;
            textBox.Loaded += textBox_Loaded;


        }
        base.OnAttached();
    }

    void textBox_Loaded(object sender, RoutedEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        if (textBox != null)
        {
            DoSpellCheck(textBox);
        }

    }

    protected override void OnDetaching()
    {
        if (AssociatedObject is TextBox)
        {
            TextBox textBox = AssociatedObject as TextBox;
            textBox.PreviewKeyUp -= OnTextBox_PreviewKeyUp;
            textBox.LostFocus -= OnTextBox_LostFocus;
            textBox.Loaded -= textBox_Loaded;
        }
        base.OnDetaching();
    }

    private static void OnTextChanged(object sender, TextChangedEventArgs e)
    {
        if (sender is TextBox)
        {
            TextBox textBox = sender as TextBox;
            if (textBox != null && textBox.IsLoaded == true)
            {
                DoSpellCheck(textBox);
            }

        }
    }

    private bool IsInputKey(Key key)
    {
        return !_nonInputKeys.Contains(key);
    }

    private void OnTextBox_PreviewKeyUp(object sender, KeyEventArgs e)
    {
        if (sender is TextBox && IsInputKey(e.Key))
        {
            TextBox textBox = sender as TextBox;

            DoSpellCheck(textBox);
        }
    }

    private void OnTextBox_LostFocus(object sender, RoutedEventArgs e)
    {
        if (sender is TextBox)
        {
            TextBox textBox = sender as TextBox;
            DoSpellCheck(textBox);
        }

    }

    public static void DoSpellCheck(TextBox textBox)
    {
        const string iDelta = "I∆n";

        int index = textBox.Text.IndexOf(iDelta);
        if (index > 0)
        {
            int iStart = textBox.GetSpellingErrorStart(index);
            if (iStart > -1)
            {
                SpellingError spellingError = textBox.GetSpellingError(iStart);
                if (spellingError != null)
                {
                    spellingError.IgnoreAll();
                }
            }
        }

    }
}