Es muy fácil vincular Buttoncorreos electrónicos en aplicaciones WPF a correos electrónicos Commanden una VIEWMODELclase. Me gustaría lograr un enlace similar para a TextBox.
Tengo un TextBoxy necesito unirlo a un Commandque se enciende cuando golpeo Entermientras el TextBoxestá enfocado. Actualmente, estoy usando el siguiente controlador para el KeyUpevento, pero se ve feo ... y no puedo ponerlo en mi VIEWMODELclase.
private void TextBox_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Key == System.Windows.Input.Key.Enter)
    {
        // your event handler here
        e.Handled = true;
        MessageBox.Show("Enter Key is pressed!");
    }
}
¿Hay una mejor manera de hacer esto?

Me enfrenté al mismo problema y encontré una solución aquí , aquí está el ejemplo de código:
<TextBox> <TextBox.InputBindings> <KeyBinding Command="{Binding Path=CmdSomething}" Key="Enter" /> </TextBox.InputBindings> </TextBox>fuente
UpdateSourceTrigger=PropertyChangedsu enlace TextBox para que esto funcione.Button.IsDefaultno ayuda mucho, si cada cuadro de texto debe activar un botón diferente y / o comando enlazado.Me gusta la respuesta de Sarh, pero no funcionaría en mi programa, a menos que cambie
EnteraReturn:<TextBox> <TextBox.InputBindings> <KeyBinding Key="Return" Command="{}" /> </TextBox.InputBindings> </TextBox>fuente
<TextBox Text="{Binding FieldThatIAmBindingToo, UpdateSourceTrigger=PropertyChanged}"> <TextBox.InputBindings> <KeyBinding Command="{Binding AddCommand}" Key="Return" /> </TextBox.InputBindings> </TextBox>Tomé respuesta desde aquí
fuente
Puede establecer true en la propiedad AcceptReturn.
<TextBox AcceptsReturn="True" />fuente