AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.04k stars 2.25k forks source link

TextPresenter is rendered before TemplateBinding of SelectionEnd to TextBox is updated when invoking SelectAll just after updating visibility #17530

Open AngryCarrot789 opened 6 days ago

AngryCarrot789 commented 6 days ago

Describe the bug

From what I can tell, it seems like the TemplateBinding used to bind a TextBox's SelectionEnd property to the TextPresenter's SelectionEnd property does not get updated quickly enough when the TextBox's SelectionEnd changes, and as a result, the TextPresenter is rendered with a previous value.

To Reproduce

Set a TextBox's IsVisible property to true (when it was previously false), then set the Text to anything, then Focus() it and then invoke SelectAll(). It will select an amount of characters equal to I assume the length of the Text before IsVisible was set to false

Expected behavior

It should select the exact number of chars

Avalonia version

11.2.999-cibuild0048720-alpha

OS

Windows

Additional context

Here is a debug screenshot, the evaluator says the TextBox's selection end is 9, but the TextPresenter's is 5 image

AngryCarrot789 commented 6 days ago

Workaround:

textBox.Focus();
textBox.SelectAll();

TextPresenter? presenter = textBox.FindDescendantOfType<TextPresenter>(false);
if (presenter != null) {
    presenter.CoerceValue(TextPresenter.SelectionStartProperty);
    presenter.CoerceValue(TextPresenter.SelectionEndProperty);
}
Gillibald commented 5 days ago

You are assuming text is also synced in time. I guess the TextPresenter still holds the old value. It was never a good idea to sync state via bindings.