如何获得没有目标类型的WPF样式(可以应用于所有对象的WPF样式)?

<Style x:Key="Basic" TargetType="???">
    <Setter Property="FontFamily" Value="Tahoma"/>
    <Setter Property="FontSize" Value="12"/>
</Style>

我希望所有其他样式都基于此“基本”样式。

问候,
MadSeb

最佳答案

附加“控件”。到属性的开头,然后删除TargetType。然后,在从其派生的样式中,将BasedOn与指向基础样式的StaticResource一起使用。

<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样式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3925170/

10-12 12:49
查看更多