如果我想在UserControl或UserControl的部分中创建TextBlock
和FontWeight="Bold"
的所有TextAlignment="Right"
元素,该怎么办?我可以为某个范围内的TextBlock
元素设置一些样式,所以我不必重复所有这些属性吗?
答案 0 :(得分:1)
是的,创建一个没有x:Key
的样式,它将应用于该范围内指定TargetType
的所有项目
例如,要使所有TextBlock仅在特定FontWeight="Bold"
中包含TextAlignment="Right"
和UserControl
,您可以使用以下内容:
<UserControl.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="TextAlignment" Value="Right" />
</Style>
</UserControl.Resources>
答案 1 :(得分:0)
如果你把它放在你的资源中,你的所有文本块都会变成相同的。
<Style TargetType="{x:Type TextBlock}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="TextAlignment" Value="Right"/>
</Style>
或者,您也可以从TextBlock
(例如,BoldTextBlock
)继承子类并将其用作目标类型。这样您就可以在与特殊文本块相同的控件中使用常规文本块
<Style TargetType="{x:Type BoldTextBlock}">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="TextAlignment" Value="Right"/>
</Style>