【问题标题】:WPF: Where are the built-in styles definedWPF:定义的内置样式在哪里
【发布时间】:2018-11-10 15:12:58
【问题描述】:

我在工具栏中有一个重复按钮。我希望它具有与工具栏按钮相同的视觉外观。但是 ToolBar.ButtonStyleKey 由于 TargetType 不同而不起作用。那么在哪里定义了内置样式(针对各种主题)以及如何为重复按钮克隆 ToolBar.ButtonStyle?

【问题讨论】:

  • 我无法帮助您解决问题的第一部分(默认样式定义在哪里?在某些主题中,它们又存储在某个 DLL 文件中,我猜。 ..)。但要获取 XAML 形式的控件的(默认)控件模板,您可以遵循此处给出的建议:*.com/a/26548268/2819245
  • 这能回答你的问题吗? Lookup default styles in wpf?

标签: wpf


【解决方案1】:

它们在C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework* 程序集中定义。您可以使用反编译器如dotPeek 来提取模板,或者您可以在Visual Studio 或Blend 中的设计模式下右键单击控件并选择Edit Template->Edit a Copy 将默认模板复制到您的XAML 标记。

我试过了,我得到了ToolBarTemplate,但没有得到 ToolBarButton 的样式。搜索结束于 ToolBarButtonStyleKey,但找不到 WPF 的 ToolBarButtonStyle。

就在那儿:

<Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="{x:Type Button}">
    <Setter Property="Control.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="Control.Padding" Value="2"/>
    <Setter Property="Control.BorderThickness" Value="1"/>
    <Setter Property="Control.Background" Value="Transparent"/>
    <Setter Property="Control.BorderBrush" Value="Transparent"/>
    <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
    <Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
    <Setter Property="Control.HorizontalContentAlignment" Value="Center"/>
    <Setter Property="Control.VerticalContentAlignment" Value="Center"/>
    <Setter Property="Control.Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Name="Bd" Background="{TemplateBinding Control.Background}"
                  BorderBrush="{TemplateBinding Control.BorderBrush}"
                  BorderThickness="{TemplateBinding Control.BorderThickness}"
                  Padding="{TemplateBinding Control.Padding}" SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
                              SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="UIElement.IsMouseOver" Value="true">
                        <Setter TargetName="Bd" Value="{StaticResource ƻ}" Property="Border.BorderBrush"/>
                        <Setter TargetName="Bd" Value="{StaticResource ƺ}" Property="Border.Background"/>
                    </Trigger>
                    <Trigger Property="UIElement.IsKeyboardFocused" Value="true">
                        <Setter TargetName="Bd" Value="{StaticResource ƻ}" Property="Border.BorderBrush"/>
                        <Setter TargetName="Bd" Value="{StaticResource ƺ}" Property="Border.Background"/>
                    </Trigger>
                    <Trigger Property="ButtonBase.IsPressed" Value="true">
                        <Setter TargetName="Bd" Value="{StaticResource ƾ}" Property="Border.BorderBrush"/>
                        <Setter TargetName="Bd" Value="{StaticResource ƽ}" Property="Border.Background"/>
                    </Trigger>
                    <Trigger Property="UIElement.IsEnabled" Value="false">
                        <Setter Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" Property="Control.Foreground"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

【讨论】:

  • 谢谢mm8,我试过了,我得到了工具栏的模板,但没有工具栏按钮的样式。搜索结束于 ToolBarButtonStyleKey,但找不到 WPF 的 ToolBarButtonStyle。
  • 谢谢!我想我必须更多地了解 dotPeek。