WPF: A TextBox that has an event that fires when the Enter Key is pressed

后端 未结 5 1787
走了就别回头了
走了就别回头了 2021-01-30 18:10

Instead of attaching a PreviewKeyUp event with each TextBox in my app and checking if the pressed key was an Enter key and then do an action, I decided

5条回答
  •  青春惊慌失措
    2021-01-30 18:39

    Since this question was asked, there is now an InputBindings property on TextBoxes and other controls. With this, a purely XAML solution can be used, rather than using custom controls. Assigning KeyBindings for Return and Enter to point to a command can do this.

    Example:

    
        
            
            
        
    
    

    Some have mentioned that Enter does not always work, and Return may be used on some systems.

提交回复
热议问题