Spellcheck only replaces first word in TextBox

2019-07-21 21:41发布

I know I have seen this problem before somewhere, but I'm not sure if there was an answer at the time. I'm trying to add SpellCheck to a TextBox in WPF, .NET 4.0. It works fine in terms of finding and marking the incorrect words, and will replace the first word in the TextBox if it's incorrect. Anything past word one though, and it just moves the carat to the start of the TextBox without changing anything? As I said I saw this somewhere about 6-9 months ago, but now everything I come up with in google deals with alternate languages (I'm staying strictly in English for now). I've included the event methods and styling XAML only for completeness, I don't think the issue lies there.

XAML:

<MultiBox:MultiBox Name="callNotes" Grid.Column="1" Width="Auto" Height="Auto" Margin="2,5,15,20" VerticalAlignment="Stretch" AcceptsReturn="True" FontWeight="Bold" GotFocus="callNotes_GotFocus" SelectAllOnGotFocus="False" SpellCheck.IsEnabled="True" xml:lang="en-US" Style="{StaticResource TextBoxStyle}" TextChanged="callNotes_TextChanged" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />

<Style x:Key="TextBoxStyle" TargetType="{x:Type MyNamespace:MultiBox}">
    <Setter Property="CharacterCasing" Value="Upper" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Top" />
    <Setter Property="Height" Value="23" />
    <Setter Property="Width" Value="Auto" />
    <Setter Property="SelectAllOnGotFocus" Value="True" />
    <Setter Property="TextWrapping" Value="Wrap" />
</Style>

Code:

private void callNotes_TextChanged(object sender, TextChangedEventArgs e)
{
    callNotes.Text.ToUpper();
    lineCountOne.Content = ((callNotes.Text.Length / 78) + 1);
}

private void callNotes_GotFocus(object sender, RoutedEventArgs e)
{
    callNotes.CaretIndex = callNotes.Text.Length;
}

2条回答
做个烂人
2楼-- · 2019-07-21 22:14

After trying jschroedl's suggestion and still having no luck (although I do know that his answer should have been correct), I started playing with every possible setting I could think of, even to the point of creating a completely new WPF project with a single, Spellcheck-enabled TextBox just to make sure it wasn't something with the Visual Studio/.NET installation itself. Turns out it wasn't, it was something I had done months ago to ensure that selecting any given TextBox through the program would result in the SelectAll() method being triggered. Once I screened out this particular TextBox from that bit of code, all works great. Again, thanks to jschroedl, I know there is no way he could have known this. The offending code is below, in case anybody comes across a similar issue.

    protected override void OnStartup(StartupEventArgs e)
    {
        EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true);

        base.OnStartup(e);
    }

    protected static void SelectAllText(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox != null && textBox.Name != "callNotes")
            textBox.SelectAll();
    }

Adding the && textBox.Name != "callNotes" solved the problem.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-07-21 22:15

It would help to see your code which attempts to correct the errors. Here's simple code which loops through all the detected errors and accepts the first suggestion. If you only want to fix particular errors, you'll need to skip to the particular error you're interested in by getting the error at a certain index.

        int ndx;
        while ((ndx = callNotes.GetNextSpellingErrorCharacterIndex(0, LogicalDirection.Forward)) != -1) 
        {
            var err = callNotes.GetSpellingError(ndx);
            foreach (String sugg in err.Suggestions)
            {
                err.Correct(sugg);
                break;
            }
        }
查看更多
登录 后发表回答