WPF TextBox按回车键执行

WPF TextBox按回车键执行,示例代码如下:
<TextBox x:Name="tbSN"  Text="{Binding materialModel.SN, Mode=TwoWay}" Width="356">
    <TextBox.InputBindings>
        <KeyBinding Command="{Binding QueryCommand}" Key="Enter" CommandParameter="{Binding ElementName=tbSN, Path=Text}"/>
    </TextBox.InputBindings>
</TextBox>

  

MVVM命令,示例代码如下:

/// <summary>
/// 查询命令
/// </summary>
private DelegateCommand<string> _queryCommand;
public DelegateCommand<string> QueryCommand
{
    get
    {
        if (_queryCommand == null)
        {
            _queryCommand = new DelegateCommand<string>(QueryCommandExecuted);
        }
        return _queryCommand;
    }
}
private void QueryCommandExecuted(string sn)
{
            
}

  

posted @ 2024-05-23 01:13  microsoft-zhcn  阅读(95)  评论(0)    收藏  举报