Search code examples
c#wpfxamlcommandbinding

WPF: Execute a Command Binding in a search field when pressing the enter button


I have a search field in my WPF app with a search button that contains a command binding. This works great, but how can i use the same command binding for the text field when pressing enter on the keyboard? The examples I have seen are all using the Code behind with a KeyDown Event Handler. Is there a smart way to make this work only with xaml and command binding?


Solution

  • You can use the IsDefault property of the button:

        <Button Command="SearchCommand" IsDefault="{Binding ElementName=SearchTextBox,
                                                   Path=IsKeyboardFocused}">
             Search!
       </Button>