在我的应用程序中,我有一堆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/