簡體   English   中英

在WPF中的多個窗口上的KeyBinding

[英]KeyBinding on multiple windows in wpf

我目前正在編程一個由主窗口(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')

我應該怎么做才能避免這個錯誤? 是否有更好的方法可在應用程序的所有窗口中使用相同的快捷方式?

在每個窗口實例中創建新的鍵綁定:

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或定義命令的任何類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM