Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind code to the TextInput event of an Avalonia TextBox?

I have a text box:

<TextBox Text="{Binding Greeting}" TextInput="OnTextInput"/>

And I'm trying, as you can see, to bind to the TextInput event so that I can do something when the user types some text. However no matter what I do, I get this error on the binding:

Unable to find suitable setter or adder for property TextInput of type Avalonia.Input:Avalonia.Input.InputElement for argument System.Private.CoreLib:System.String, available setter parameter lists are: System.EventHandler`1[[Avalonia.Input.TextInputEventArgs, Avalonia.Input, Version=0.10.0.0, Culture=neutral, PublicKeyToken=c8d484a7012f9a8b]]

I've tried defining a method called OnTextInput on my view model, and also on the code-behind for the view containing the text box. It looks like this:

public void OnTextInput(object sender, TextInputEventArgs e)
{
}

I also tried using RoutedEventArgs in place of TextInputEventArgs. But no matter what I do, I still get that error. How can I set up this binding so that I can do something when the user types some text?

like image 891
ekolis Avatar asked Nov 15 '25 00:11

ekolis


1 Answers

Having sought out a replacement for the not-yet-added TextChanged event, which the original question is about, I found a workaround using the KeyUp (effectively KeyPress event) to do the same thing. I'm not using MVVM as I am just making a simple Form to use as a replacement for a WinForm. This is from my Login Form's code:

// This requires using Avalonia.Input; 
private void txtPassword_KeyPressUp(object sender, KeyEventArgs e) {
  if (txtPassword.Text == null)
     return;
  Password = txtPassword.Text;
  if (txtPassword.Text.Trim().Length > 6) {
     btnOK.IsEnabled = true;
  } else {
     btnOK.IsEnabled = false;
  }
}

Which is in the code-behind class, LoginForm.axaml.cs file. The LoginForm.axaml definition for the textbox txtPassword is as follows (the axaml for the button is not included.):

<TextBox x:Name="txtPassword" Watermark="Password..." PasswordChar="*"  Width="220" Height="36" KeyUp="txtPassword_KeyPressUp" />

Behind the scenes, in my initialization code, I create a TextBox control in code, and link it to the axaml control (as a reference) via this code snippet:

NameScope thisWindowNameScope = (NameScope)this.FindNameScope();
txtPassword = (TextBox)thisWindowNameScope.Find("txtPassword");

I'm answering this, mainly because I would have loved to have seen this, when I first found this question. So, after making and testing the above code, I am posting it here to help someone else out. Like I have said before, great framework, crappy documentation. To be fair, it is under construction and I look forward to seeing both styles of coding given decent examples. (One doesn't need MVVM if it's a small program, larger programs are a different story.)

like image 132
CherryCoke Avatar answered Nov 17 '25 19:11

CherryCoke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!