WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

TextBox overwrites text when filtering in ViewModel #20219

@wfee2000

Description

@wfee2000

Describe the bug

When trying to filter or change the text inside of a TextBox when the user types new characters the TextBox ignores the real text in the ViewModel. Similar behavior as #3603.

To Reproduce

Create a new MVVM Application

ViewModel:

public partial class MainWindowViewModel : ViewModelBase
{
    private string _greeting = "HELLO HOW ARE YALL";

    public string Greeting
    {
        get => _greeting;
        set => SetProperty(ref _greeting, value.Replace("L", "K"));
    }
}

MainWindow:

<Window .... >
        <TextBox 
        Name="Test" 
        Text="{Binding Greeting}"/>
</Window>

For logging MainWindow.axaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Test.PropertyChanged += (_, args) =>
        {
            if (args.Property.Name == "Text")
            {
                Console.WriteLine($"old: {args.OldValue}, new: {args.NewValue}");
            }
        };
    }
}

Output:

// first set of the property no change
old: , new: HELLO HOW ARE YALL
// third set from the ViewModel replacing L by K
old: HELLO HOW ARE YALLB, new: HEKKO HOW ARE YAKKB
// second set from TextBox adding the Input
old: HELLO HOW ARE YALL, new: HELLO HOW ARE YALLB
// fourth set overwrites the correct text - I could not figure out from where 
old: HEKKO HOW ARE YAKKB, new: HELLO HOW ARE YALLB
Image

When using Backspace or delete the fourth set does not occur and therefore this works fine in those cases.

Expected behavior

It should not have fired the fourth set to achieve text filtration and with that following output:

// first set of the property no change
old: , new: HELLO HOW ARE YALL
// third set from the ViewModel replacing L by K
old: HELLO HOW ARE YALLB, new: HEKKO HOW ARE YAKKB
// second set from TextBox adding the Input
old: HELLO HOW ARE YALL, new: HELLO HOW ARE YALLB
Image

Avalonia version

11.3.9

OS

Linux

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions