简体   繁体   English

在generic.xaml中定义的覆盖样式将导致合并样式

[英]overriding styles defined in generic.xaml results in merged style

I have a control with a style defined in a separate resource dictionary and use the generic.xaml magic to apply it. 我有一个在单独的资源字典中定义的样式的控件,并使用generic.xaml魔术应用了它。

If I understand the lookup mechanism described on msdn ( https://msdn.microsoft.com/de-de/library/ms750613%28v=vs.110%29.aspx ), the generic.xaml is used after the application resources, but adding a style for MyWindow will result in the style from generic.xaml + the style defined in App.xaml. 如果我了解msdn( https://msdn.microsoft.com/de-de/library/ms750613%28v=vs.110%29.aspx )上描述的查找机制,则在应用程序资源之后使用了generic.xaml,但是为MyWindow添加样式将导致通用样式xaml + App.xaml中定义的样式。

Here is my code: 这是我的代码:

Generic.xaml Generic.xaml

<ResourceDictionary ...>
   <Style TargetType="{x:Type test:MyWindow}" BasedOn="{StaticResource ResourceKey={x:Type Window}}">
        <Setter Property="Background" Value="Gainsboro" />
        <Setter Property="Title" Value="Default!" />
   </Style>
</ResourceDictionary>

App.xaml App.xaml中

<Application.Resources>
     <ResourceDictionary>
         <Style TargetType="{x:Type test:MyWindow}" BasedOn="{StaticResource ResourceKey={x:Type Window}}">
            <Setter Property="Background" Value="HotPink" />
         </Style>
</Application.Resources>

The window will have a pink background (from the application.resource style) and "Default!" 该窗口将具有粉红色背景(来自application.resource样式)和“默认!” as title from the generic.xaml style. 作为generic.xaml样式的标题。

Why doesn't wpf stop searching for a style at the application level? wpf为什么不停止在应用程序级别搜索样式?

This because default (theme) styles are treated differently from normal styles. 这是因为默认(主题)样式与普通样式不同。

Consider the Dependency Property lookup precedence list : 考虑“ 依赖项属性”查找优先级列表

  1. Property system coercion. 财产制度的强制。
  2. Active animations. 活动动画。
  3. Local value. 本地价值。
  4. TemplatedParent properties. TemplatedParent属性。 Triggers and property sets from the TemplatedParent. 来自TemplatedParent的触发器和属性集。
  5. Implicit style. 隐式风格。 A special case for the Style property. Style属性的一种特殊情况。 Here, the Style property is filled by any style resource with a key that matches the type of that element. 在此, Style属性由任何样式资源填充,且其键与该元素的类型匹配。 This lookup does not proceed into the themes . 此查询不会涉及主题
  6. Style triggers. 样式触发器。 Triggers within styles from page or application. 在页面或应用程序的样式内触发。
  7. Template triggers. 模板触发器。
  8. Style setters. 样式设置器。
  9. Default (theme) style. 默认(主题)样式。
  10. Inheritance. 遗产。
  11. Default value from dependency property metadata. 依赖项属性元数据中的默认值。

When WPF is deciding the value for MyWindow.Style , it goes through the precedence list and settles on using "5. implicit style" to assign it. 当WPF确定MyWindow.Style的值时,它将通过优先级列表并决定使用“ 5.隐式样式”进行分配。 So then it finds a matching style in App.xaml and uses that. 因此,然后在App.xaml中找到匹配的样式并使用它。 If you inspect MyWindow's properties at runtime, you should indeed see that MyWindow.Style is set to the one from App.xaml. 如果在运行时检查MyWindow的属性,则实际上应该看到MyWindow.Style已设置为App.xaml中的MyWindow.Style So, WPF actually does stop searching for a style at the application level. 因此,WPF实际上确实停止了在应用程序级别上搜索样式。

It's just that because of the DefaultStyleKeyProperty , the default style is still there in the DependencyProperty lookup list, albeit at a lower precedence level than the App.xaml style. 只是由于DefaultStyleKeyProperty ,所以DependencyProperty查找列表中仍存在默认样式,尽管其优先级低于App.xaml样式。

In this case, the App.xaml doesn't set the Title property, so the DependencyProperty engine falls back to the default style in Generic.xaml to provide a value. 在这种情况下,App.xaml不会设置Title属性,因此DependencyProperty引擎将退回到Generic.xaml中的默认样式以提供一个值。 So this is why you are getting that merged style behaviour. 因此,这就是您获得合并样式行为的原因。

Of course, note that this only happens when the Generic.xaml magic is set up properly . 当然,请注意,只有在正确设置 Generic.xaml魔术后,才会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM