c# - wpf中多个窗口上的KeyBinding
问题描述
我目前正在编写一个由主窗口组成的应用程序(这里的 DataContext 是我的一个类,MainWindowController),我在其中定义了这个 KeyBinding:
<Window.InputBindings>
<KeyBinding Key="N" Modifiers="Control" Command="{Binding Path=NewProgramCommand}"/>
<KeyBinding Key="O" Modifiers="Control" Command="{Binding Path=OpenProgramCommand}"/>
<KeyBinding Key="S" Modifiers="Control" Command="{Binding Path=SaveProxyCommand}"/>
<KeyBinding Key="W" Modifiers="Control" Command="{Binding Path=CloseProxyCommand}"/>
</Window.InputBindings>
我可以从我的主窗口打开其他窗口。我已经定义了一个抽象类 AbstractWindow: Window 来处理应用程序关闭等。我的另一个窗口被创建为 AbstractWindows
<window:AbstractWindow x:Class="FinancialViewModule.FinancialView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
Height="800" Width="1280"
d:DesignHeight="300" d:DesignWidth="300">
当用户单击按钮时,命令会创建新窗口。
mController.FinancialView = new FinancialView();
mController.FinancialView.Show();
我希望我的 KeyBindings 可以在所有窗口中工作,所以在 AbstractWindow 构造函数中,我有:
InputBindings.AddRange(Application.Current.MainWindow.InputBindings);
快捷方式可在 FinancialView 窗口中使用,但输出中有错误:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=NewProgramCommand; DataItem='MainWindowController' (HashCode=37314933); target element is 'KeyBinding' (HashCode=38008833); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=OpenProgramCommand; DataItem='MainWindowController' (HashCode=37314933); target element is 'KeyBinding' (HashCode=5210297); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=SaveProxyCommand; DataItem='MainWindowController' (HashCode=37314933); target element is 'KeyBinding' (HashCode=34357331); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=CloseProxyCommand; DataItem='MainWindowController' (HashCode=37314933); target element is 'KeyBinding' (HashCode=41051448); target property is 'Command' (type 'ICommand')
我能做些什么来避免这个错误?有没有更好的方法让应用程序的所有窗口都使用相同的快捷方式?
解决方案
在每个窗口实例中创建新的键绑定:
KeyBinding n = new KeyBinding() { Modifiers = ModifierKeys.Control, Key = Key.N };
KeyBinding o = new KeyBinding() { Modifiers = ModifierKeys.Control, Key = Key.O };
KeyBinding s = new KeyBinding() { Modifiers = ModifierKeys.Control, Key = Key.S };
KeyBinding w = new KeyBinding() { Modifiers = ModifierKeys.Control, Key = Key.W };
BindingOperations.SetBinding(n, InputBinding.CommandProperty, new Binding("NewProgramCommand"));
BindingOperations.SetBinding(o, InputBinding.CommandProperty, new Binding("OpenProgramCommand"));
BindingOperations.SetBinding(s, InputBinding.CommandProperty, new Binding("SaveProxyCommand"));
BindingOperations.SetBinding(w, InputBinding.CommandProperty, new Binding("CloseProxyCommand"));
InputBindings.AddRange(new KeyBinding[4] { n, o, s, w });
...并确保将每个窗口设置DataContext
为MainWindowController
定义命令的类型或任何类型。
推荐阅读
- ruby - 如何测试 ruby 的初始化方法,该方法又调用私有方法
- r - left_join 列表中跨多个数据帧的数据帧
- elasticsearch - 未能在索引中匹配/搜索
- tensorflow - 在 tf.data 输入管道中使用 tf.function 构建自定义地图函数
- php - 通过 cronjob 运行 PHP 脚本并避免重复的 PHP 进程(flock)
- flutter - 如何进行自定义转换并保持 ios 向后滑动
- python - 转置表
- python - 如何以指数形式将混淆矩阵中显示的值修复为正常形式
- solr - SOLR:相同的品牌面与不同的外壳
- python - 如何使用python将df更新为现有的excel文件