Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RichEditBox raises TextChanged event when spellcheck detects misspelt word #9898

Closed
JuliusZet opened this issue Aug 14, 2024 · 3 comments
Closed
Labels
area-TextBox TextBox, RichEditBox bug Something isn't working closed-ByDesign Described behavior is by design. team-Controls Issue for the Controls team

Comments

@JuliusZet
Copy link

JuliusZet commented Aug 14, 2024

Describe the bug

The Microsoft.UI.Xaml.Controls.RichEditBox raises its TextChanged event when the spellcheck detects a misspelt word even though the text did not actually change.

Steps to reproduce the bug

To demonstrate the issue, create a new blank WinUI 3 app project in Visual Studio and put the following code in your MainWindow.xaml and MainWindow.xaml.cs files:

MainWindow.xaml:

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="RichEditBox_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:RichEditBox_Test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <StackPanel Margin="8">
        <TextBlock Text="Please enter some text in the RichEditBox below, then press the SetText() button."/>
        <RichEditBox x:Name="_richEditBox"/>
        <Button Content="SetText()" Click="ButtonSetText_OnClick"/>
        <TextBlock x:Name="_textBlockInfo"/>
    </StackPanel>
</Window>

MainWindow.xaml.cs:

using Microsoft.UI.Text;
using Microsoft.UI.Xaml;

namespace RichEditBox_Test
{
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void ButtonSetText_OnClick(object sender, RoutedEventArgs e)
        {
            this._richEditBox.TextChanged -= this.RichEditBox_OnTextChanged;
            this._textBlockInfo.Text = "";

            this._richEditBox.Document.SetText(TextSetOptions.None, "This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times. This is just some random text, that is spelled correctly and will now repeat 100 times.\r\nBut this word here is not spelled correctly: qw\r\n");
            
            this._richEditBox.TextChanged += this.RichEditBox_OnTextChanged;

            // Please note, that we subscribed to the TextChanged event after changing the text.
        }

        private void RichEditBox_OnTextChanged(object sender, RoutedEventArgs e)
        {
            this._textBlockInfo.Text = "TextChanged event raised!";
        }
    }
}

The issue is only reproducible, when the user has written some text in the RichEditBox before. I assume, that spellcheck does not kick in, when the user has never written any text in the RichEditBox before.

So, please enter some text in the RichEditBox and then press the SetText() button. This will replace the text in the RichEditBox with some very long text with a misspelt word at the end and then subscribe an event-handler to the TextChanged event. The purpose for the wall of text before the misspelt word is to create some delay, so that the issue is easier to see.

You will see, that the TextChanged event will be raised as soon as the spellcheck detects and underlines the misspelt word "qw".

Expected behavior

When spellcheck detects a misspelt word, it should not raise a TextChanged event, because the text did not actually change.

Screenshots

Edit: https://youtu.be/cTotf9H2LXw

NuGet package version

WinUI 3 - Windows App SDK 1.5.6: 1.5.240802000

Windows version

Windows 10 (21H2): Build 19044

Additional context

No response

@JuliusZet JuliusZet added the bug Something isn't working label Aug 14, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Aug 14, 2024
@codendone codendone added area-TextBox TextBox, RichEditBox team-Controls Issue for the Controls team and removed needs-triage Issue needs to be triaged by the area owners labels Aug 16, 2024
@ranjeshj
Copy link
Contributor

ranjeshj commented Sep 3, 2024

@JuliusZet This seems to be the current behavior. One way to work around this might be look at the TextChanging event whose args contain IsContentChanging to notify if the content is actually changing. Would that help your scenario?

@ranjeshj ranjeshj added the needs-author-feedback Asked author to supply more information. label Sep 3, 2024
@JuliusZet
Copy link
Author

@ranjeshj thanks for the workaround! :) I now subscribe to the TextChanging event instead of the TextChanged event. And within the event-handler for the TextChanging event I can simply put my code in an if-statement, like this:

        private void RichEditBox_OnTextChanging(RichEditBox sender, RichEditBoxTextChangingEventArgs args)
        {
            if (args.IsContentChanging)
            {
                // Do stuff here
            }
        }

I can confirm, that this works.

But it would still be great if the issue could be fixed anyway at some point in time.

Thanks again and have a great day! :)

BR JuliusZet

@microsoft-github-policy-service microsoft-github-policy-service bot added needs-triage Issue needs to be triaged by the area owners and removed needs-author-feedback Asked author to supply more information. labels Sep 5, 2024
@codendone codendone removed the needs-triage Issue needs to be triaged by the area owners label Sep 12, 2024
@llongley
Copy link
Member

llongley commented Nov 8, 2024

I believe this is by design. The WPF counterpart to this explicitly says that it's raised when any formatting changes, which would include the spellcheck underline:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.primitives.textboxbase.textchanged?view=windowsdesktop-8.0#system-windows-controls-primitives-textboxbase-textchanged

@ranjeshj 's suggested workaround is how I believe you are meant to detect change to the text itself.

@llongley llongley closed this as not planned Won't fix, can't repro, duplicate, stale Nov 8, 2024
@llongley llongley added the closed-ByDesign Described behavior is by design. label Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TextBox TextBox, RichEditBox bug Something isn't working closed-ByDesign Described behavior is by design. team-Controls Issue for the Controls team
Projects
None yet
Development

No branches or pull requests

4 participants