wpf - 以隐式样式设置 SystemColors 覆盖

标签 wpf xaml resources styles

在我的应用程序中,我有一堆ContextMenus,我希望它们都具有相同的外观,这是非常基本的,但它使用资源来设置HighlightBrushKey和ControlBrushKey,它们是SystemColors。它看起来像这样:

<ContextMenu Padding="0" Background="Transparent">
    <ContextMenu.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
    </ContextMenu.Resources>
    <MenuItem Header="Delete"/>
    <MenuItem Header="Modify"/>
</ContextMenu>

这里没有什么太奇特的东西,但我找不到一种方法来将它放入样式中,我想做的是:

<Style TargetType="ContextMenu">
    <Setter Property="Padding" Value="0" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Resources">
        <Setter.Value>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        </Setter.Value>
    </Setter>
</Style>

你如何将资源放在一种风格中? (如果可能的话...)

谢谢!

最佳答案

您无法通过 setter 设置资源,因为它不是依赖项属性。将相关资源添加到 Style.Resources 或覆盖 Template 并在其中添加资源。但范围可能有限。


<Style TargetType="ContextMenu">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
    </Style.Resources>
    <Setter Property="Padding" Value="0" />
    <Setter Property="Background" Value="Transparent" />
</Style>

关于wpf - 以隐式样式设置 SystemColors 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7255416/

相关文章:

wpf - 如何使用触发器更改 TextBlock 的前景色?

wpf - WTF WPF TabControl?

php - 148 是不是包含的文件太多

wpf - 如何从 MEF 组件提供 XAML 资源

c# - DragEnter 和 DragLeave : Avoid nasty flickering on custom drag preview

wpf - 在 WPF 中将对象绑定(bind)到窗口文本框

鼠标悬停和鼠标移出时的 wpf 工具提示

c# - Xamarin 表单 : How to create a dynamic IMarkupExtension?

c# - 如何绑定(bind)到 XAML 中实例化类的属性

java - 如果程序在没有关闭流的情况下关闭,FileInputStream 访问的文件会发生什么情况?