关于这个问题(Multiple styles on same target type)我还有一个关于一般wpf样式规则的观点 .

如上面的一个答案中所述,当没有指定x:key时,wpf将最接近的样式定义应用于控件 . (也适用于具有相同键的样式;假设我们在此处具有相同的键:控件类型)

我的问题:如果我们在不同的资源字典中有两个同名的样式,它们都是相同的,并且可以被控件访问?

与CSS相比,您有两个定义,即a元素 . 在CSS中,相同的样式属性被最后一个定义覆盖,而不同的属性被合并到一个逻辑样式定义中 .

WPF的行为是否相同,或者它是否更喜欢具有所有属性的整个样式资源,而完全避免使用另一个样式资源?

如果是后者,它如何决定应用其中一个 .

更新

dict1.xaml:

<ResourceDictionary>
 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
  <Setter Property="BorderBrush" Value="SomeValue1" />
  <Setter Property="Background" Value="SomeValue2" />
  <Setter Property="FontSize" Value="SomeValue3" />
 </Style>
</ResourceDictionary>

dict2.xaml:

<ResourceDictionary>
 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
  <Setter Property="BorderBrush" Value="SomeValue4" />
  <Setter Property="Foreground" Value="SomeValue5" />
 </Style>
</ResourceDictionary>

SomeView.xaml:

...
<ResourceDictionary.MergedDictionaries>
 <ResourceDictionary Source="path/dict1.xaml" />
 <ResourceDictionary Source="path/dict2.xaml" />
</ResourceDictionary.MergedDictionaries>

<Grid>
 <Grid.Resources>
  <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
  <Setter Property="FontSize" Value="SomeValue6" />
  </Style>
 </Grid.Resources>

 <Button /><!--THIS IS THE CONTROL THAT MATTERS-->
</Grid>
...

我认为对于FontSize来说它很简单,它适用于SomeValue6,因为它是最接近的样式定义 . 但是BorderBrush怎么样?两个字典都与Button非常接近,并且都可以访问 .

背景和前景怎么样?它是否将两种样式定义合并为:

<ResourceDictionary>
 <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
  <Setter Property="BorderBrush" Value="SomeValue4" />
  <Setter Property="Background" Value="SomeValue2" />
  <Setter Property="Foreground" Value="SomeValue5" />
  <Setter Property="FontSize" Value="SomeValue6" />
 </Style>
</ResourceDictionary>

当然,当你包含相互依赖的跨组装词典并且你正在寻找实际风格来自哪里时,混乱会更大 .

这个代码示例由于合并的字典而无法正常工作,因为我的实际应用程序真的像我描述的那样混乱 .

所以我想知道微软的官方规则应该是每个开发人员的基本规则 . 但我在文档中找不到任何内容 .