2009-05-03 79 views
12

而不是在我的应用程序中附加PreviewKeyUp事件与每个TextBox事件,并检查按下的键是否为回车键,然后执行操作,我决定实现TextBox的扩展版本,该版本包含在TextBox中按Enter键时触发的DefaultAction事件。WPF:按下回车键时触发事件的文本框

我所做的是基本上创建一个新的类,从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,我发现几乎大部分时间有一个“冷”(希望更容易)WA y执行的东西

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

回答

23

看看this blog post从几个月前我附加一个'全球'事件处理程序到TextBox.GotFocus选择文本。

从本质上讲,你可以在你的App类处理KeyUp事件,像这样:

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。我相信这可以让你得到你想要的。

+0

但有了这个,你如何分配时,按下Enter键时会发生什么?因为每个TextBox都有不同的作用 – 2009-05-03 10:43:57

+0

哦!我会更新答案。 – 2009-05-03 11:41:53

+0

最佳答案! – 2014-02-20 11:51:27

12

由于此问题被问到,TextBoxes和其他控件上现在有一个InputBindings属性。借此,可以使用纯粹的XAML解决方案,而不是使用自定义控件。指定KeyBinding s为ReturnEnter指向一个命令可以做到这一点。

例子:

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

有些人提到,Enter并不总是工作,并Return可能在某些系统中使用。在xaml

0
private void txtBarcode_KeyDown(object sender, KeyEventArgs e) 
    { 
     string etr = e.Key.ToString(); 

     if (etr == "Return") 
     { 
      MessageBox.Show("You Press Enter"); 
     } 
    } 
0

活动添加到特定的文本框或对象:

KeyDown="txtBarcode_KeyDown"

0

当用户按下在TextBox回车键,在文本框中输入出现的另一个区域用户界面(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文件

相关问题