2013-01-23 108 views
0

我在C#WPF中有一個簡單的需求。我有一個文本框,我將在其中輸入一些文本。當我按下Enter鍵時,我想要一個事件被解僱。我的代碼是在文本框中讀入文本後點擊進入C#WPF

private void AddKeyword(object sender, KeyEventArgs e) 
    { 
    if(e.Key == Key.Enter) 
     { 
     //DO something 
     } 
    } 

我已經設置了我的文本框AcceptReturn = True;該方法根本不工作,我沒有看到任何事件發生時,我按Enter鍵。請幫助我。由於提前

+0

是訂閱了KeyUp事件的AddKeyword? –

+0

是.. AddKeyword訂閱了KeydDown事件。其他鍵是射擊事件。僅當按Enter鍵時,它保持空閒狀態。它會在文本框 – Vasanth91

+0

中啓動一個新的文本行,而不是發生事件觸發,請分享您的代碼,以幫助查找問題! –

回答

2

這項工作對我罰款

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="textBox1" VerticalAlignment="Top" Width="190" KeyUp="textBox1_KeyUp" /> 
    </Grid> 
</Window> 

using System.Windows; 
using System.Windows.Input; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void textBox1_KeyUp(object sender, KeyEventArgs e) 
     { 
      if (e.Key == Key.Enter) 
      { 
       e.Handled = true; 
       MessageBox.Show("Enter Fired!"); 
      } 
     } 
    } 
} 
+0

響應的關鍵答案它也適用於我..謝謝:) – Vasanth91

相關問題