簡體   English   中英

WPF (Bug) FocusVisualStyle 不適用於帶有模板的 Window

[英]WPF (Bug) FocusVisualStyle doesn't work for Window with Template

我遇到了FocusVisualStyle的問題,我需要覆蓋TAB 鍵盤焦點樣式,當我覆蓋Window.Template all controls styles FocusVisualStyle on the Z05B8C74CBD96FBF2DE4C1A352702FFF4

問題 XAML (選項卡樣式不起作用):

<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="450">
    <Window.Style>
        <Style TargetType="Window">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Style>
    <Window.Resources>
        <Style x:Key="ButtonTabFocused" TargetType="Control">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="-3" StrokeThickness="3" Stroke="Red" SnapsToDevicePixels="true"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid Background="White">
        <Button
            Width="100"
            Height="40"
            Content="MyButton"
            FocusVisualStyle="{StaticResource ButtonTabFocused}" />

    </Grid>
</Window>

在此處輸入圖像描述

沒有Window.Style FocusVisualStyle效果很好(標簽樣式有效):

<Window
    x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="450">
    <Window.Resources>
        <Style x:Key="ButtonTabFocused" TargetType="Control">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Rectangle Margin="-3" StrokeThickness="3" Stroke="Red" SnapsToDevicePixels="true"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid Background="White">
        <Button
            Width="100"
            Height="40"
            Content="MyButton"
            FocusVisualStyle="{StaticResource ButtonTabFocused}" />

    </Grid>
</Window>

在此處輸入圖像描述

沒有建立 msdn 信息。 您能否告訴我任何解決方法,或者我錯過了什么導致這種行為的原因。

我的環境:

  • .NET 框架 4.7.2(舊版本相同)
  • Windows 10 19042.685
  • 視覺工作室 2019

謝謝。

更新,下面的工作方式,感謝 arxont:

    <Style TargetType="Window">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Grid>
                        <AdornerDecorator>
                            <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
                        </AdornerDecorator>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

您需要添加 AdornerDecorator。 更改您的 window 樣式:

<AdornerDecorator>
    <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />                       
</AdornerDecorator>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM