在WPF+WMMV模式中使用键盘和鼠标事件的绑定代码如下:

<TextBox x:Name=”SearchBox” Text=”{Binding SearchText}” Width=”246″ Height=”24″ HorizontalAlignment=”Right” PreviewKeyDown=”SearchBox_OnKeyDown“>
             <TextBox.InputBindings>
                   <KeyBinding Command=”{Binding KeyEventCommand}” Key=”Enter”/>//绑定键盘输入事件

            <dxg:GridControl.InputBindings>

                <MouseBinding Command=”{Binding ProductDoubleClickCommand}” CommandParameter=”{Binding ElementName=ProductCtrl,Path=CurrentItem}”                                                                   MouseAction=”LeftDoubleClick”/>//绑定鼠标事件

           </dxg:GridControl.InputBindings>

             </TextBox.InputBindings>
 </TextBox>

上面需要注意的是:搜索文本框的输入文本在按Enter后虽然会触发事件,但是获取不到搜索文本框的输入文本值,因此需要是搜索文本框的输入文本在按Enter后失去焦点,

多以添加PreviewKeyDown=”SearchBox_OnKeyDown,

 private void SearchBox_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SearchBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }


版权声明:本文为LongtengGensSupreme原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/LongtengGensSupreme/article/details/77040364