【发布时间】:2014-07-02 13:02:08
【问题描述】:
我有一个包含 TabControl 的 WPF 窗口,其中包含显示页面的框架。像这样:
<Window x:Class="MyNamespace.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<DockPanel>
<TabControl x:Name="TabControl">
<TabItem Header="StartScreen" x:Name="StartScreenTab">
<Frame Source="StartScreenPage.xaml"/>
</TabItem>
<TabItem Header="OtherTab" x:Name="OtherTab">
<Frame Source="OtherPage.xaml"/>
</TabItem>
</TabControl>
</DockPanel>
</Window>
在其中一个页面中,我有一个 KeyBinding:
<Page x:Class="MyNamespace.View.OtherPage"
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"
mc:Ignorable="d"
Title="OtherPage">
<Page.InputBindings>
<KeyBinding Key="U" Modifiers="Control" Command="{Binding MyCommand}"/>
</Page.InputBindings>
<!-- Content ... -->
</Page>
MyCommand 是OtherPage.DataContext 的一个属性,它只能从页面获得,不能从外部获得。
我的问题是 KeyBinding 仅在我单击页面内的控件后才起作用。我希望 KeyBinding 在 OtherPage 可见时工作,等效地当 OtherTab 是活动选项卡时。我怎样才能做到这一点?
【问题讨论】:
标签: c# wpf focus key-bindings