【问题标题】:WPF KeyBinding on Page in Frame (in TabControl)框架中页面上的 WPF 键绑定(在 TabControl 中)
【发布时间】: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>

MyCommandOtherPage.DataContext 的一个属性,它只能从页面获得,不能从外部获得。

我的问题是 KeyBinding 仅在我单击页面内的控件后才起作用。我希望 KeyBinding 在 OtherPage 可见时工作,等效地当 OtherTab 是活动选项卡时。我怎样才能做到这一点?

【问题讨论】:

标签: c# wpf focus key-bindings


【解决方案1】:

实现这一目标的唯一方法是将 KeyBindingICommand 实现移至 MainWindow.xaml 文件:

<Window.InputBindings>
    <KeyBinding Key="U" Modifiers="Control" Command="{Binding MyCommand}"/>
</Window.InputBindings>

如果您无法移动ICommand 的实际实现,那么您至少必须使其可以从那里访问。

【讨论】:

    【解决方案2】:

    我做了类似于 Sheridan 在他的answer 中推荐的事情。

    MainWindow.xaml 我声明一个RoutedUICommand

    <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">
        <Window.Resources>
            <ResourceDictionary>
                <RoutedUICommand x:Key="OtherPageCommand"/>
            </ResourceDictionary>
        </Window.Resources>
        <Window.CommandBindings>
            <CommandBinding Command="{StaticResource OtherPageCommand}"
                            Executed="OtherPageCommandExecuted"/>
        </Window.CommandBindings>
        <Window.InputBindings>
            <KeyBinding Key="U" Modifiers="Control" Command="{StaticResource OtherPageCommand}"/>
        </Window.InputBindings>
    <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>
    

    MainWindow.xaml.cs我有这个方法:

        private void OtherPageCommandExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            var page = ((Frame)((TabItem) TabControl.SelectedItem).Content).Content as OtherPage;
            if (page != null)
            {
                var viewModel = page.DataContext as MyViewModel;
                if (viewModel != null)
                {
                    var openWindow = viewModel.MyCommand;
                    var parameter = null; // put your command parameter here
                    if (openWindow.CanExecute(parameter))
                        openWindow.Execute(parameter);
                }
            }
        }
    

    这样我将键绑定传播到页面,但实际命令仍保留在页面的视图模型中。

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 2018-04-21
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-10-27
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      相关资源
      最近更新 更多