放置在内容演示者中时,WPF按钮焦点样式将被覆盖

时间:2017-06-29 21:01:03

标签: wpf xaml contentpresenter

设置

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

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

我用它来自定义窗口周围的窗口镶边(我删除了那部分),因此窗口的所有实际内容都进入了内容展示器。我像这样使用这种风格:

<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会生成一个如下所示的窗口:

Test window

问题

在普通窗口中,当用户通过控件切换时,突出显示当前按钮以向用户显示当前按钮。请注意下面第3个按钮周围的边框。

enter image description here

出于某种原因,当我使用我的风格时,此功能消失并且没有任何关于哪个按钮具有焦点的指示,即使用户可以像平常那样使用标签。这是为什么以及如何恢复内置功能。

请注意

我将这种风格与数十个窗口和数百个控件一起使用。我不想在每个控件上使用一个样式,触发器在控件具有焦点时显示边框。我想恢复在应用自定义窗口样式之前使用的默认功能。

1 个答案:

答案 0 :(得分:1)

您只需要为wmsSector.addEventListener("click",turnon('wmsSector',sector)) ContentPresenter定义为AdornerDecorator,它就可以正常运行。我起初并没有弄明白:显然:)

样式;

Window

和你的窗口......

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

希望这会有所帮助,欢呼。