以隐式样式设置 SystemColors 覆盖
在我的应用程序中,我有一堆 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>
如何将资源放入样式中? (如果可能的话...)
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法通过 setter 设置
Resources
,因为它不是依赖项属性。将相关资源添加到Style.Resources
或覆盖Template
并在其中添加资源。但范围可能有限。You cannot set
Resources
via a setter as it is not a dependency property. Add the relevant resources to theStyle.Resources
or override theTemplate
and add resources there. The scope may be limited though.