【问题标题】:WPF: Two TabControls / Switching works under Win7/.NET 4.5 but not under Windows 8.1/.NET 4.5.1WPF:两个 TabControls / Switching 在 Win7/.NET 4.5 下有效,但在 Windows 8.1/.NET 4.5.1 下无效
【发布时间】:2014-10-16 17:26:06
【问题描述】:

我有一个带有两个 TabControl 的窗口。一个是左对齐,另一个是右对齐。 TabControls 的 Headers 在顶行对齐。

每当我单击一个 TabItem 时,另一个 TabControl 都没有焦点。

这在我的开发 PC(Windows 7、.NET Framework 4.5)上运行良好。

但是,当我在装有 Windows 8.1 (.NET 4.5.1) 的 PC 下执行它时,我无法切换到右对齐的 TabControl。当我点击其中的 TabItem 时,什么都没有发生。

主窗口的 XAML:

    <Window x:Class="TabGroupProblem.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" Loaded="Window_Loaded">
        <Grid>
            <TabControl GotFocus="TabControl_GotFocus" x:Name="c1">
                <TabItem Header="One">h</TabItem>
                <TabItem Header="Two">i</TabItem>
            </TabControl>

            <TabControl GotFocus="TabControl_GotFocus_1" x:Name="c2">
                <TabControl.Resources>
                    <Style TargetType="{x:Type TabPanel}">
                        <Setter Property="HorizontalAlignment" Value="Right" />
                    </Style>
                </TabControl.Resources>
                <TabItem Header="Three">j</TabItem>
                <TabItem Header="four">k</TabItem>
            </TabControl>
        </Grid>
    </Window>

事件处理程序:

    private void TabControl_GotFocus(object sender, RoutedEventArgs e)
    {
        // c1 to foreground
        System.Windows.Controls.Grid.SetZIndex(c2, -1);
        c2.SelectedIndex = -1;
    }

    private void TabControl_GotFocus_1(object sender, RoutedEventArgs e)
    {
        // c2 to foreground
        System.Windows.Controls.Grid.SetZIndex(c2, 1);
        c1.SelectedIndex = -1;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Controls.Grid.SetZIndex(c1, 0);
        System.Windows.Controls.Grid.SetZIndex(c2, -1);
        c2.SelectedIndex = -1;
    }

为什么会发生这种情况,我该怎么做?

编辑: 在我的开发 PC 上将 Target 框架切换到 .NET 4.5.1。在我的 PC 上切换 TabControls 仍然有效(在 Windows 8.1 PC 上仍然无效)

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    您将两个 TabControl 直接放置在彼此之上,但仍希望能够聚焦下方的一个。这显然恰好在 Win7 中工作,但似乎 Windows 8 使用了不同的控件模板,该模板可能会吃掉鼠标点击 TabPanel 的“空白”位。

    可能是添加了透明笔刷,所以你可以尝试一个简单的:

    <TabControl.Resources>
        <Style TargetType="{x:Type TabPanel}">
            <Setter Property="Background" Value="{x:Null}" />
        </Style>
    </TabControl.Resources>
    

    但是,我怀疑放弃选项卡控件并自己创建一些样式按钮会更容易?

    【讨论】:

    • 很遗憾,这行不通。我猜按钮会在“堆叠”的画布/网格之间切换? (所以我得把每个TabItem都转成这样一个层?)
    猜你喜欢
    • 2013-11-23
    • 2011-04-04
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-10
    • 2013-12-21
    • 1970-01-01
    相关资源
    最近更新 更多