开发者

WPF Style with no target type?

开发者 https://www.devze.com 2023-01-20 11:30 出处:网络
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=\"???\">

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 开发者_JAVA技巧Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="12"/>
</Style>

I want to base all other styles on this "basic" style.

Regards, MadSeb


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号