Setting SystemColors overrides in an implicit style

走远了吗. 提交于 2020-01-02 11:05:13

问题


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

Thanks!


回答1:


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>


来源:https://stackoverflow.com/questions/7255416/setting-systemcolors-overrides-in-an-implicit-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!