全局风格和本地风格在同一控制下覆盖全局风格

时间:2014-04-22 04:40:21

标签: c# wpf xaml resourcedictionary wpf-style

我在DefaultTheme.xaml文件中为TypeMenu Item声明了一个全局样式

<Style TargetType="{x:Type MenuItem}">
    .
    .
    .
</Style>

将其合并到用户控件

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary  Source="../DefaultTheme.xaml" />
</ResourceDictionary.MergedDictionaries>

现在在用户控件中如果我为TargetType="{x:Type MenuItem}"声明任何样式,它将继承从全局样式声明的样式。

E.g。

<Style  x:Key="LocalStyle" TargetType="{x:Type MenuItem}">
    .
    .
    .
</Style>

这也将应用全局样式。我不希望这种本地风格继承全球风格。所以我可以通过在我的usercontrol中提供<Style TargetType="{x:Type MenuItem}">..</Style>来覆盖它。

这是捕获,如果我这样做,我将无法将全局样式应用于控件中的其他位置,因为我已在本地覆盖此。应如何处理?

1 个答案:

答案 0 :(得分:0)

根据Style Inheritance,你的叙述是相反的。由于本地样式不是从基础继承的,因此基本样式不会应用于本地。

If I do this i will not be able to apply the global style to other places in the control because I have locally overridden this

这不会发生,因为本地样式是使用键声明的,它将应用于引用键的MenuItem。其余全部采用全球风格。

相关问题