WPF资源的继承

假设这里有一个全局的资源

<Style TargetType="TextBlock">
    <Setter Property="FontSize" Value="40"/>
</Style>

这是时候有些控件可能需要一个样式在这个基础上加一点内容的

<Style x:Key="textBlockStyle" TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
    <Setter Property="Foreground" Value="Yellow"/>
</Style>

        --其实这个TargetType="TextBlock"是TargetType="{x:Type TextBlock}"的简写

如果是普通有key的就basedon后面接静态资源就行了

<Style x:Key="textBlockStyle1" TargetType="TextBlock" BasedOn="{StaticResource textBlockStyle}">
    <Setter Property="Foreground" Value="Yellow"/>
</Style>

拓展:Sytle设置TargetType的作用就是方便给Setter设置属性,因为不同控件有不同的属性。要是没有用TargetType指定是哪个控件,Setter设置属性的时候需要加上控件的限定名:“TextBlock.Property="TextBlock.Foreground”。

相关推荐

  1. WPF资源继承

    2024-03-13 04:06:02       44 阅读
  2. WPF.NET开发】WPFXAML资源

    2024-03-13 04:06:02       45 阅读
  3. C#WPF自定义控件-继承Button圆角按钮

    2024-03-13 04:06:02       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-13 04:06:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-13 04:06:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-13 04:06:02       87 阅读
  4. Python语言-面向对象

    2024-03-13 04:06:02       96 阅读

热门阅读

  1. 前端算法之希尔排序

    2024-03-13 04:06:02       39 阅读
  2. ImportError: cannot import name ‘URL’ from ‘sqlalchemy’

    2024-03-13 04:06:02       43 阅读
  3. Linux:安装docker并修改其目录

    2024-03-13 04:06:02       33 阅读
  4. 安卓 修改系统时间

    2024-03-13 04:06:02       38 阅读
  5. `PF_NETLINK` 是用于与内核通信的Socket族之一

    2024-03-13 04:06:02       41 阅读
  6. effective c++ 笔记 条款49-52

    2024-03-13 04:06:02       36 阅读
  7. 【笔记】道路不平度的功率谱密度计算时的问题

    2024-03-13 04:06:02       42 阅读
  8. MogDB/openGauss关于PL/SQL匿名块调用测试

    2024-03-13 04:06:02       38 阅读
  9. 从菜鸟到大师细看程序员的五种层次

    2024-03-13 04:06:02       40 阅读