放置在内容展示器中时,WPF 按钮焦点样式被覆盖 [英] WPF button focus style being overriden when placed in content presenter

查看:24
本文介绍了放置在内容展示器中时,WPF 按钮焦点样式被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以下样式应用于我的应用程序中的大多数窗口:

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>

上面的这个 XAML 产生一个看起来像这样的窗口:

This XAML above produces a window that looks like this:

在普通窗口中,当用户使用 Tab 键浏览控件时,突出显示当前按钮以向用户显示当前按钮.请注意下方第三个按钮周围的边框.

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.

出于某种原因,在我使用我的样式的那一刻,此功能消失了,并且没有指示哪个按钮具有焦点,即使用户可以像平常一样使用 Tab.为什么会这样,我如何才能恢复内置功能.

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.

我将此样式用于数十个窗口和数百个控件.我不想在每个控件上使用样式,并在控件具有焦点时显示边框.我想恢复在应用自定义窗口样式之前使用的默认功能.

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.

推荐答案

您只需要将 ContentPresenter 定义为 AdornerDecorator for Window> 它将按预期工作.显然我一开始也没有弄清楚:)

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 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>

还有你的窗户...

<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.

这篇关于放置在内容展示器中时,WPF 按钮焦点样式被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆