4

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?

7
  • 1
    Try to use Avalonia.Input.TextInputEventArgs e or using Avalonia.Input;. This should fix the error you get. Commented Feb 13, 2021 at 9:01
  • However feel free to comment again, because I think TextInput does not what you would like to achieve. Instead I think you want to check for changed Text on your Binding. I could provide an anwer to that... Commented Feb 13, 2021 at 9:04
  • Yes, I have using Avalonia.Input. Changed text on my binding? How would I do that? Commented Feb 13, 2021 at 18:38
  • Your code works for me (Compiles without the error). Maybe your Window is not properly attached to your CodeBehind? Commented Feb 13, 2021 at 19:04
  • Oh, I see, the event handlers need to go in the code behind, not the view model. Not sure why that wasn't working before, maybe I needed to build the project to make them see each other? Now I'm trying to figure out how I can make the UI react in response to user input... I don't see any way to make things happen in the UI from the code behind; there must be some way to access either the controls in the UI or the view model from this context... Commented Feb 13, 2021 at 19:46

1 Answer 1

4

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.)

Sign up to request clarification or add additional context in comments.

1 Comment

A pull request adding a TextChanged event was just merged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.