繁体   English   中英

框架中页面上的WPF键绑定(在TabControl中)

[英]WPF KeyBinding on Page in Frame (in TabControl)

我有一个包含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的属性,仅在页面上可用,而在外部则不可用。

我的问题是,只有在单击Page内部的控件之后,KeyBinding才起作用。 我希望只要在OtherPage可见时,即当OtherTab是活动选项卡时,该KeyBinding就可以工作。 我该如何实现?

您可以实现的唯一方法是将KeyBindingICommand实现移至MainWindow.xaml文件:

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

如果您不能移动ICommand的实际实现,则必须至少使它可以从那里访问。

我所做的事情与谢里丹在他的回答中建议的类似。

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);
            }
        }
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM