WPF/XAML:使用不同的 TargetType 设置样式?
我在使用 x:Key 引用的资源字典中有一个外部样式资源.它有一个 x:TargetType 指定一个目标 (TextBlock).是否可以将其应用于包含 TextBlock 的控件并让该控件中的所有 TextBlock 元素都应用该样式?
I have an external style resource in a resource dictionary I'm referencing with x:Key. It has an x:TargetType specifying a target (TextBlock). Is it possible to apply this to a control containing a TextBlock and have all TextBlock elements within that control have the style applied?
谢谢,罗伯特
最简单的方法是在控件中定义一个基于外部样式资源的样式,但不要指定 x:Key,只是 TargetType.
The easiest way to do that would be to define a Style within the Control that is based on your external style resource, but don't specify an x:Key, just the TargetType.
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource SomeOtherStyle}">
如果没有键,它将应用于控件内的所有文本块.
Without a key, it'll apply itself to all TextBlocks within the control.