WPF键绑定在帧中的页面(在TabControl中)

本文关键字:TabControl WPF 绑定 | 更新日期: 2023-09-27 18:11:12

我有一个包含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是活动选项卡时。我怎样才能做到这一点呢?

WPF键绑定在帧中的页面(在TabControl中)

您可以实现这一点的唯一方法是将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);
            }
        }
    }

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