设置SystemColors覆盖隐式样式
问题描述:
在我的应用程序中,我有一堆ContextMenus,我希望它们都具有相同的外观,这是非常基本的,但它使用资源来设置HighlightBrushKey和ControlBrushKey,它们是systemColors中。它看起来像这样:设置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设置Resources
,因为它不是依赖项属性。将相关资源添加到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>
这是卓有成效的,我这个时候,我不知道我是否有目标控制设置,虽然资源将如何表现。 –