简体   繁体   English

使用InputBindings时如何避免多行TextBox Enter键被吞咽?

[英]How to avoid multiline TextBox Enter keypress is swallowed when using InputBindings?

I'm doing WPF MVVM and have the following XAML code: 我正在做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>

When handling the Enter press in the ViewModel's command property (initialized in constructor with event handler added): 处理Enter时,请按一下ViewModel的command属性(在构造函数中初始化,并添加事件处理程序):

private readonly MyCommand myCommand;
public ICommand MyCommand
{
  get
  {
    return myCommand;
  }
}

private void OnCommand(object sender, MyCommandEventArgs e)
{
  <do something>
}

The event handler is called allright, but the Enter keypress is never passed back to the TextBox and the newline is not added (the cursor stays on the same line). 事件处理程序称为“正常”,但是Enter键不会被传递回TextBox,并且不会添加换行符(光标停留在同一行)。

Doesn't event bubbling ensure that the Enter keypress gets passed on to the TextBox? 事件冒泡是否可以确保Enter键被传递给TextBox?

Could you just add "\\n" to your textbox' Text-value? 您可以在文本框的“文本值”中添加“ \\ n”吗?

(Would have add that as a comment but I may not do that yet...) (将其添加为评论,但我可能还没有这样做...)

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

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