如何避免多行文本框输入键吞掉时使用InputBindings

本文关键字:InputBindings 输入 何避免 文本 | 更新日期: 2023-09-27 17:53:42

我正在做WPF MVVM,并有以下XAML代码:

<TextBox Grid.Row="1" Name="Console" MinWidth="500" HorizontalAlignment="Stretch" AcceptsReturn="True" CharacterCasing="Upper" TextWrapping="WrapWithOverflow" Cursor="IBeam">
  <TextBox.InputBindings>
    <KeyBinding Command="{Binding Path=MyCommand}" CommandParameter="{Binding Text, ElementName=Console}" Key="Enter"/>
  </TextBox.InputBindings>
</TextBox>

当在ViewModel的命令属性中处理Enter键时(在构造函数中初始化并添加事件处理程序):

private readonly MyCommand myCommand;
public ICommand MyCommand
{
  get
  {
    return myCommand;
  }
}
private void OnCommand(object sender, MyCommandEventArgs e)
{
  <do something>
}

事件处理程序被称为allright,但是回车键永远不会传递回TextBox,并且不添加换行符(光标保持在同一行上)。

事件冒泡确保回车键传递到文本框?

如何避免多行文本框输入键吞掉时使用InputBindings

你可以在你的文本框' Text-value '中添加"'n"吗?