本文介绍了没有目标类型的WPF样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何拥有没有目标类型的WPF样式(可以应用于所有对象的样式)?
How can I have a WPF style that has no target type ( one that can be applied to all objects) ?
<Style x:Key="Basic" TargetType="???">
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="12"/>
</Style>
我希望所有其他款式都基于这种基本风格。
I want to base all other styles on this "basic" style.
问候,
MadSeb
Regards,MadSeb
推荐答案
追加控制。到属性的开头,并删除TargetType。然后,在从它派生的样式中,使用带有指向基本样式的StaticResource的BasedOn。
Append "Control." to the beginning of the Property, and remove the TargetType. Then, in the styles that derive from it, use BasedOn with a StaticResource pointing at the base Style.
<Style x:Key="basicStyle">
<Setter Property="Control.FontFamily" Value="Tahoma" />
<Setter Property="Control.FontSize" Value="12" />
</Style>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource basicStyle}">
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource basicStyle}">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource basicStyle}">
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Margin" Value="2,4" />
</Style>
这篇关于没有目标类型的WPF样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!