WPF中添加一个文本输入框,按Enter回车,执行绑定的Command
在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));
}
}
相关文章
- 8天入门wpf—— 第七天 画刷
- wpf开源界面收集
- WPF System.InvalidCastException: 无法将类型为“System.Windows.Media.Color”的对象强制转换为类型“System.Windows.Media.Brush”。
- WPF 自定义TreeView控件样式,仿QQ联系人列表
- wpf 菜单
- 编写 WPF DataGrid 列模板,实现更好的用户体验
- wpf 客户端【JDAgent桌面助手】开发详解(三) 瀑布流效果实现与UI虚拟化优化大数据显示
- WPF的二维绘图(二)——几何图形Geometry
- Chart FX for WPF 8.1 Crack
- PDFView4NET 11.0.6 Windows Forms and WPF
- MvvmCross 跨平台应用开发——进阶篇_WPF导航局部切换