简体   繁体   English

如何将代码绑定到 Avalonia TextBox 的 TextInput 事件?

[英]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.如您所见,我正在尝试绑定到TextInput事件,以便在用户键入一些文本时我可以做一些事情。 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]]无法为参数 System.Private.CoreLib:System.String 的 Avalonia.Input:Avalonia.Input.InputElement 类型的属性 TextInput 找到合适的设置器或加法器,可用的设置器参数列表为:System.EventHandler`1[[Avalonia.Input. TextInputEventArgs,Avalonia.Input,版本=0.10.0.0,文化=中性,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.我尝试在我的视图 model 以及包含文本框的视图的代码隐藏上定义一个名为OnTextInput的方法。 It looks like this:它看起来像这样:

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

I also tried using RoutedEventArgs in place of TextInputEventArgs .我还尝试使用RoutedEventArgs代替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?如何设置此绑定,以便在用户键入某些文本时可以执行某些操作?

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.在为原始问题所涉及的尚未添加的 TextChanged 事件寻找替代品后,我找到了一种使用 KeyUp(实际上是 KeyPress 事件)来做同样事情的解决方法。 I'm not using MVVM as I am just making a simple Form to use as a replacement for a WinForm.我没有使用 MVVM,因为我只是在制作一个简单的表单来替代 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.这是在代码隐藏 class, LoginForm.axaml.cs 文件中。 The LoginForm.axaml definition for the textbox txtPassword is as follows (the axaml for the button is not included.):文本框 txtPassword 的 LoginForm.axaml 定义如下(不包括按钮的 axaml。):

<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:在后台,在我的初始化代码中,我在代码中创建了一个 TextBox 控件,并通过以下代码片段将其链接到 axaml 控件(作为参考):

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.公平地说,它正在建设中,我期待看到 styles 的编码给出了不错的例子。 (One doesn't need MVVM if it's a small program, larger programs are a different story.) (如果它是一个小程序,则不需要 MVVM,更大的程序是另一回事。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM