本文介绍了以隐式样式设置 SystemColors 覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application, I have a bunch of ContextMenus, and I want them all to have the same look, which is quite basic, but it uses the resources to set HighlightBrushKey and ControlBrushKey, which are SystemColors. It looks like this:

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

这里没有什么太花哨的东西,但我找不到一种方式来把它放在一种风格中,我想做的是:

Nothing too fancy here, but I can't find a way to put it in a style, what I would like to do is something along the lines of:

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

你如何以一种风格来放置资源?(如果可能的话……)

How do you put resources in a style? (if it is at all possible...)

谢谢!

推荐答案

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

You cannot set Resources via a setter as it is not a dependency property. Add the relevant resources to the Style.Resources or override the Template and add resources there. The scope may be limited though.

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

这篇关于以隐式样式设置 SystemColors 覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 08:06