在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));
}
}