没有目标类型的WPF样式?

标签 wpf inheritance styles

如何获得没有目标类型的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/

相关文章:

java - 如何在不将我的类绑定(bind)到特定实现的情况下获得实现继承的好处?

android - 主题不适用于 Android 上的 DialogFragment

c# - WPF 应用程序中的 MVVM 符合本地化

c# - 以编程方式在文本之间制作带有超链接的文本 block

c# - 如何将焦点授予已创建的窗口而不是创建新实例?

wpf - 在 Setter.Value 结构中设置事件处理程序

android - 不同的主题可以有不同的color.xml吗?

c# - WPF MVVM 可观察集合不更新 GUI

java - 模型类本身用作字段类型会导致 Ebean 中出现错误

Swift:初始化程序不会覆盖其父类(super class)中的指定初始化程序