23

我没有在我的应用程序中为每个事件附加一个PreviewKeyUp事件TextBox并检查按下的键是否是 Enter 键然后执行操作,而是决定实现 a 的扩展版本,TextBox其中包括一个 DefaultAction 事件,该事件在 a 中按下 Enter 键时触发TextBox

我所做的基本上是创建一个从TextBox公共事件扩展的新类DefaultAction,如下所示:

public class DefaultTextBoxControl:TextBox
{
    public event EventHandler<EventArgs> DefaultAction = delegate { };

    public DefaultTextBoxControl()
    {
        PreviewKeyUp += DefaultTextBoxControl_PreviewKeyUp;
    }

    void DefaultTextBoxControl_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key != Key.Enter)
        {
            return;
        }
        DefaultAction(this, EventArgs.Empty);
    }
}

然后我使用我的应用程序中的这个自定义文本框,比如(xaml):

<Controls:DefaultTextBoxControl  DefaultAction="DefaultTextBoxControl_DefaultAction">
</Controls:DefaultTextBoxControl>

现在,在我学习 WPF 的一点经验中,我意识到几乎大多数时候都有一种“更酷”(并且希望更容易)的方式来实现事物

...所以我的问题是,如何改善上述控制? 或者也许有另一种方法可以进行上述控制?...也许只使用声明性代码而不是声明性(xaml)和程序性(C#)?

4

5 回答 5

45

看看几个月前的这篇博客文章TextBox.GotFocus,我在其中附加了一个“全局”事件处理程序来选择文本。

本质上,您可以KeyUp在 App 类中处理事件,如下所示:

protected override void OnStartup(StartupEventArgs e)
{
    EventManager.RegisterClassHandler(typeof(TextBox),
        TextBox.KeyUpEvent,
        new System.Windows.Input.KeyEventHandler(TextBox_KeyUp));

    base.OnStartup(e);
}

private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key != System.Windows.Input.Key.Enter) return;

    // your event handler here
    e.Handled = true;
    MessageBox.Show("Enter pressed");
}

...现在您的应用程序中的每个人都会在用户输入TextBox时调用该方法。TextBox_KeyUp

更新

正如您在评论中指出的那样,这仅在每个人都TextBox需要执行相同的代码时才有用。

要添加像 Enter 按键这样的任意事件,您最好查看Attached Events。我相信这可以得到你想要的。

于 2009-05-03T08:11:31.037 回答
31

既然问了这个问题,现在InputBindingsTextBoxes 和其他控件上有一个属性。有了这个,可以使用纯粹的 XAML 解决方案,而不是使用自定义控件。将 s分配KeyBindingReturnEnter指向一个命令可以做到这一点。

例子:

<TextBox Text="Test">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding SomeCommand}" Key="Return" />
        <KeyBinding Command="{Binding SomeCommand}" Key="Enter" />
    </TextBox.InputBindings>
</TextBox>

有些人提到这Enter并不总是有效,并且Return可能在某些系统上使用。

于 2014-12-21T16:25:41.610 回答
7

当用户在 TextBox 中按下 Enter 键时,文本框中的输入将出现在用户界面 (UI) 的另一个区域中。

以下 XAML 创建用户界面,该界面由 StackPanel、TextBlock 和 TextBox 组成。

<StackPanel>
  <TextBlock Width="300" Height="20">
    Type some text into the TextBox and press the Enter key.
  </TextBlock>
  <TextBox Width="300" Height="30" Name="textBox1"
           KeyDown="OnKeyDownHandler"/>
  <TextBlock Width="300" Height="100" Name="textBlock1"/>
</StackPanel>

下面的代码创建 KeyDown 事件处理程序。如果按下的键是 Enter 键,则会在 TextBlock 中显示一条消息。

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Return)
    {
        textBlock1.Text = "You Entered: " + textBox1.Text;
    }
}

有关更多信息,请阅读MSDN文档

于 2018-01-16T07:49:20.810 回答
-1
    private void txtBarcode_KeyDown(object sender, KeyEventArgs e)
    {
        string etr = e.Key.ToString();

        if (etr == "Return")
        {
            MessageBox.Show("You Press Enter");
        }
    }
于 2017-10-03T02:08:13.887 回答
-2

将事件添加xaml到特定的文本框或对象:

KeyDown="txtBarcode_KeyDown"

于 2017-10-03T02:13:38.317 回答