我在输入命令键绑定方面遇到了一个问题。在这里我解释一下我的情况......我已经绑定了输入命令键绑定,如下所示,
<Window x:Class="DefaultBehavior_KeyBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.InputBindings>
<KeyBinding Key="F2" Command="{Binding TestCommand}"/>
</Window.InputBindings>
<Grid>
<Button Content="Add" Width="100" Height="35" Grid.Row="0" Name="EmptyButton" Click="EmptyButton_Click"/>
</Grid>
它工作正常。然后我在按钮单击事件中打开了新的 wpf 子窗口,如下所示,
public partial class MainWindow : Window
{
public ICommand TestCommand { get; private set; }
public MainWindow()
{
this.TestCommand = ........some command is attached here...
InitializeComponent();
this.DataContext = this;
}
private void EmptyTabButton_Click(object sender, RoutedEventArgs e)
{
Window childwindow = new Window() { Title = "ChildWindow", Width = 200, Height = 300 };
childwindow.Show();
}
}
打开子窗口后,当子窗口获得焦点时,与主窗口的键绑定不起作用。如果我将焦点切换到主窗口意味着,它很好。
我知道,主窗口和子窗口都是相互独立的。
但我的问题是,当我只将键绑定绑定到主窗口时,我怎样才能使它成为工作我的子窗口的焦点。我不想将此绑定设置到每个子窗口,因为在我的情况下,我使用了很多子窗口。
有人请给我你的建议吗?