Avalonia: Implement TextChanged event for TextBox

Created on 11 Feb 2016  路  5Comments  路  Source: AvaloniaUI/Avalonia

enhancement

Most helpful comment

I've looked into this, but the TextChanged event is quite complex and a lot of it relates to rich text edit controls, which we don't have yet. For this reason I'm going to delay implementing this.

If you want something similar for now, just do:

textBox.GetObservable(TextBox.TextProperty).Subscribe(text => { /* Will be called with Text each time it's changed */ });

All 5 comments

I've looked into this, but the TextChanged event is quite complex and a lot of it relates to rich text edit controls, which we don't have yet. For this reason I'm going to delay implementing this.

If you want something similar for now, just do:

textBox.GetObservable(TextBox.TextProperty).Subscribe(text => { /* Will be called with Text each time it's changed */ });

What's the status on this?

My use case is running custom input validation while the user is typing. I also want to deny the input if it's determined invalid, restoring the TextBox to the state before the Text has Changed. I can store the old value in a string outside the TextBox for this purpose, and change the Text Property from inside my handler method.

However, if I attempt to use the Observable workaround above, I'm unable to set the Text from inside the subscribed method (I've set it, but it does not update). This is possible in WPF with the TextChanged event.

@mat1jaczyyy worth looking at ReactiveUI framework and the MVVM pattern. Using the Binding markup extension with TwoWay binding mode and WhenAnyValue will definitely do what you are willing to achieve, see an example. For complex scenarios which support validation messages take a look at ReactiveUI.Validation.

I've managed to make my use case work by resetting the Text via a Dispatcher Invoke, but thanks for the suggestions!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

x2bool picture x2bool  路  4Comments

TheColonel2688 picture TheColonel2688  路  3Comments

CreateLab picture CreateLab  路  3Comments

Urgau picture Urgau  路  3Comments

maxkatz6 picture maxkatz6  路  3Comments