在WPF中的多个窗口上的KeyBinding

时间:2018-08-13 06:57:48

标签: c# wpf xaml key-bindings

我目前正在编程一个由主窗口(DataContext是我的主窗口控制器的类)组成的应用程序,我在其中定义了此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窗口中使用,但是在Output中出现错误:

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')

我该怎么做才能避免此错误?是否有更好的方法可以在应用程序的所有窗口中使用相同的快捷方式?

1 个答案:

答案 0 :(得分:0)

在每个窗口实例中创建新的键绑定:

var value = document.getElementById('keySkills');
console.log(variable.value)

...并确保将每个窗口的console.log(variable) 设置为<textarea class="inputbox" tabindex="23" rows="5" name="keySkills" id="keySkills" style="width: 400px;" value="Configuration of controlling area (CO), Maintaining CO version and Maintaining number ranges for planning and actual transactions in CO and Defining primary and secondary Cost Element"></textarea> 或定义命令的任何类型。