WPF button focus style being overriden when placed

2019-08-11 00:45发布

The Setup

I have the following style that I apply to most of the windows in my application:

<ResourceDictionary x:Class="MyNamespace.ChromeWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="CustomChromeTest" TargetType="{x:Type Window}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Grid x:Name="GridMain" Background="{StaticResource MainFormColor}">
                        <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

I use it to customize the window chrome around my window (I have removed that part) so all the actual contents of the window go inside the content presenter. I use this style like so:

<Window x:Class="MyNamespace.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="Window1" Height="300" Width="300"
    Style="{DynamicResource CustomChromeTest}">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/WPFControlLibrary;component/Resources/ChromeWindow.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<StackPanel>
    <Button Margin="5" Content="Button"  Width="75"/>
    <Button Margin="5" Content="Button"  Width="75"/>
    <Button Margin="5" Content="Button"  Width="75"/>
    <Button Margin="5"  Content="Button"  Width="75"/>

</StackPanel>

This XAML above produces a window that looks like this:

Test window

The Problem

In a normal window, when the user is tabbing through the controls, the current button is highlighted to show the user the current button. Note the border around the 3rd button below.

enter image description here

For some reason, the moment I use my style, this feature disappears and there is no indication of which button has focus, even though the user can tab like normal. Why is this and how can I restore the built-in functionality.

Please Note

I use this style with dozens of windows and hundreds of controls. I do not want to use a style on every control with a trigger that displays a border when the control has focus. I want to restore the default functionality that was being used before I applied my custom window style.

1条回答
迷人小祖宗
2楼-- · 2019-08-11 01:45

You'll just need to define the ContentPresenter as AdornerDecorator for Window and it will work as intended. Which I didn't figure out at first either apparently :)

Style;

<Style x:Key="CustomChromeTest" TargetType="{x:Type Window}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type Window}">
            <Grid x:Name="GridMain" Background="Yellow">
              <AdornerDecorator>                    
                <ContentPresenter/>
              </AdornerDecorator>
            </Grid>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

and your window...

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Name="Testeroo" 
        x:Class="WpfApplication1.MainWindow" 
        mc:Ignorable="d" Style="{StaticResource CustomChromeTest}"
        Title="MainWindow" Height="550" Width="750">

       <StackPanel>
           <Button Margin="5" Content="Button"  Width="75"/>
           <Button Margin="5" Content="Button"  Width="75"/>
           <Button Margin="5" Content="Button"  Width="75"/>
           <Button Margin="5"  Content="Button"  Width="75"/>
       </StackPanel>

</Window>

Hope this helps, cheers.

查看更多
登录 后发表回答