WPF/XAML:使用不同的 TargetType 设置样式? [英] WPF/XAML: Set a style with a different TargetType?

查看:33
本文介绍了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.

这篇关于WPF/XAML:使用不同的 TargetType 设置样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆