style的继承

第一种方式:瞄准控件的基类

如下例所示,继承ContentControl的控件,都可以使用这个Style

<Window.Resources>
    <Style x:Key="ContentControlStyle" TargetType="ContentControl">
        <Setter Property="Background" Value="Pink"></Setter>
        <Setter Property="FontSize" Value="12"></Setter>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <Label Style="{StaticResource ContentControlStyle}" Content="按钮和文本框使用了基于ContentControl的Style"></Label>
    <Button Style="{StaticResource ContentControlStyle}" Content="按钮" Grid.Row="1"></Button>
</Grid>

第二种方式:继承

<Window.Resources>
    <Style x:Key="baseStyle" TargetType="Button">
        <Setter Property="Background" Value="Pink"></Setter>
        <Setter Property="FontSize" Value="12"></Setter>
    </Style>
    <Style x:Key="childStyle" TargetType="Button" BasedOn="{StaticResource baseStyle}">
        <Setter Property="FontSize" Value="50"></Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource childStyle}" Content="按钮"></Button>
</Grid>

就近原则:父类子类同时设定属性,调用子类,以子类为准

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Style 的StyleInheritDemo和StyleInheritDemo2

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,469评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,207评论 4 61
  • 在一个深秋的早晨,他坐在公司的九楼办公室,喝着秘书送来的拿铁咖啡,过了五分钟的时候,办公室的门敲响了,赵总,你今天...
    efe3691c071a阅读 4,434评论 0 0
  • 和亮靓.27d5《赠诗张琦峰》 今天早上看到简友亮靓27d5的《赠诗张琦峰》,因有同感,故和之。 张张诗篇...
    林木成荫阅读 3,468评论 10 32
  • 三行诗 | 老爸 你不善言谈 也没多少文化 但无论见谁都笑哈...
    大贵贵丿阅读 3,493评论 2 7