WPF依赖属性优先级

2023-11-07

有下面代码,属性,样式,触发器都设置了按钮的颜色,请问按钮最终是什么颜色?

<Button Background="Red">
      <Button.Style>
        <Style TargetType="{x:Type Button}">
          <Setter Property="Background" Value="Green"/>
          <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="Blue" />
            </Trigger>
          </Style.Triggers>
        </Style>
      </Button.Style>
    </Button>

要解决上面的问题,需要判断依赖属性优先级,我们查阅MSDN,

以下摘自MSDNhttps://docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/dependency-property-value-precedence#animations

Dependency Property Setting Precedence List

The following is the definitive order that the property system uses when assigning the run-time values of dependency properties. Highest precedence is listed first. This list expands on some of the generalizations made in the Dependency Properties Overview.

  1. Property system coercion. For details on coercion, see Coercion, Animation, and Base Value later in this topic.

  2. Active animations, or animations with a Hold behavior. In order to have any practical effect, an animation of a property must be able to have precedence over the base (unanimated) value, even if that value was set locally. For details, see Coercion, Animation, and Base Value later in this topic.

  3. Local value. A local value might be set through the convenience of the "wrapper" property, which also equates to setting as an attribute or property element in XAML, or by a call to the SetValue API using a property of a specific instance. If you set a local value by using a binding or a resource, these each act in the precedence as if a direct value was set.

  4. TemplatedParent template properties. An element has a TemplatedParent if it was created as part of a template (a ControlTemplate or DataTemplate). For details on when this applies, see TemplatedParent later in this topic. Within the template, the following precedence applies:

    1. Triggers from the TemplatedParent template.

    2. Property sets (typically through XAML attributes) in the TemplatedParent template.

  5. Implicit style. Applies only to the Style property. The Style property is filled by any style resource with a key that matches the type of that element. That style resource must exist either in the page or the application; lookup for an implicit style resource does not proceed into the themes.

  6. Style triggers. The triggers within styles from page or application (these styles might be either explicit or implicit styles, but not from the default styles, which have lower precedence).

  7. Template triggers. Any trigger from a template within a style, or a directly applied template.

  8. Style setters. Values from a Setter within styles from page or application.

  9. Default (theme) style. For details on when this applies, and how theme styles relate to the templates within theme styles, see Default (Theme) Styles later in this topic. Within a default style, the following order of precedence applies:

    1. Active triggers in the theme style.

    2. Setters in the theme style.

  10. Inheritance. A few dependency properties inherit their values from parent element to child elements, such that they need not be set specifically on each element throughout an application. For details see Property Value Inheritance.

  11. Default value from dependency property metadata. Any given dependency property may have a default value as established by the property system registration of that particular property. Also, derived classes that inherit a dependency property have the option to override that metadata (including the default value) on a per-type basis. See Dependency Property Metadata for more information. Because inheritance is checked before default value, for an inherited property, a parent element default value takes precedence over a child element. Consequently, if an inheritable property is not set anywhere, the default value as specified on the root or parent is used instead of the child element default value.

 

以上很清楚了, 从高到底,分别是限制(Coerce),动画,本地值,模板父类(TemplatedParent),样式触发器,模板触发器,样式设置,主题样式,属性值继承,元素默认值。

所以,按钮的颜色是红色!

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WPF依赖属性优先级 的相关文章

随机推荐

  • 【计算机毕业设计】237校园招聘系统

    一 系统截图 需要演示视频可以私聊 摘要 随着信息技术在管理上越来越深入而广泛的应用 实现基于SSM框架的校园招聘系统的设计与实现在技术上已成熟 本文介绍了基于SSM框架的校园招聘系统的设计与实现的开发全过程 通过分析企业对于基于SSM框架
  • unity编程实践-HitUFO改进

    作业要求 游戏有 n 个 round 每个 round 都包括10 次 trial 每个 trial 的飞碟的色彩 大小 发射位置 速度 角度 同时出现的个数都可能不同 它们由该 round 的 ruler 控制 每个 trial 的飞碟有
  • 0706--用replace来替换用例中的字段,如手机号码或ID

    第第第 第43个视频讲解 coding utf 8 Time 2021 6 23 11 37 AUTHOR 菜菜同学 SOFTWARE lemon1 1 在EXCEL的用例中 mark规则 值 使用这个来表示当前字段需要进行替换 2 在用例
  • CH340串口驱动(包含各系统平台)

    CH340转串口芯片支持的平台驱动齐全 支持 Windows Linux Android MacOS WinCE 等操作系统 各平台下驱动官网链接和说明如下 各平台的安装与使用问题可参见其他博文 Windows驱动 下载链接 CH340 C
  • Spring学习笔记:基于XML文件和注解两种配置方式实现spring框架的IOC和DI

    首先打开IntelliJ IDEA 创建一个Maven项目spring lesson 删除src文件夹 只保留maven依赖对应的pom文件 这个项目作为父工程 在pom文件中增加
  • filco蓝牙键盘配对流程_无线化浪潮,几款最值得推荐的无线机械键盘

    在外设中相比于游戏鼠标和耳机 键盘对于无线的需求性是最弱的 毕竟键盘放在那一般比较固定 不会像鼠标在使用时线材的拖拽影响移动 耳机的连接线会增加重量 这些增加的重量全都要头部去承担 游戏间隙的内急需要取下等干扰 这也导致在无线化的普及度上键
  • Dev-c++函数的分文件编写

    首先创建一个文件侠 到时候创建的文件地址路径能一样 方便查找 c语言和c 语言雷同 2 新建 项目 3 c项目和c 项目 你用那个语言写就选那个 4 把创建的 文件2 dev 文件 保存在刚刚创建的文件下面 5 然后选择New Fie创建文
  • Lua基础之math(数学函数库)

    Lua5 1中数学库的所有函数如下表 math pi 为圆周率常量 3 14159265358979323846 abs 取绝对值 math abs 15 15 acos 反余弦函数 math acos 0 5 1 04719755 asi
  • 企业工作效率提升系统

    企业工作效率提升系统 自动化办公系统 项目介绍 框架介绍 部署流程 项目截图 小编联系方式 备注 系统名称 自动化办公系统 办公自动化 OA 是面向组织的日常运作和管理 员工及管理者使用频率最高的应用系统 极大提高公司的办公效率 项目介绍
  • hive窗口函数最全总结

    准备工作 一 窗口函数概况 1 1 窗口函数说明 1 2 窗口范围说明 1 2 1 窗口范围取值可选项 1 2 2 默认窗口范围含义 思考一 如何理解省略order by的情况 不能指定窗口范围 二 窗口函数分类和特性 2 1 窗口函数分类
  • C++算法之深度优先搜索算法

    深度优先搜索算法是图算法的一种 即DFS Depth First Search 其过程是对每个可能的分支路径深入直到不能再深入为止 下面会介绍深度优先搜索算法 目录 1 框架 2 过程 2 1 步骤 2 2 解释 3 例题 1 框架 voi
  • ROS知识点——生成点云,发布、订阅ROS点云话题

    文章目录 1 点云基本概念 1 1 点云结构公共字段 1 2 点云类型 1 3 ROS的PCL接口 1 4 pcl ros点云格式转换 2 创建点云并发布ROS点云话题 2 1 创建功能包 2 2 发布ROS点云话题 2 3 订阅ROS点云
  • 【MySQL】12-常见数据类型

    常见类型 原则 一 整型 特点 zerofill 默认无符号 二 小数 1 浮点型 2 定点型 三 字符型 1 较短 2 enum枚举型 3 set型 四 日期型 datetime 和 datestamp 区别
  • No suitable driver found for jdbc:mysql://localhost:3306/test?characterEncoding=UTF8连接不上MySQL解决方法

    No suitable driver found for jdbc mysql localhost 3306 test useUnicode true characterEncoding UTF 8 解决方法 困惑了两天的问题终于解决了 第
  • 还不懂mock测试?一篇文章带你熟悉mock

    每天进步一点点 关注我们哦 每天分享测试技术文章 本文章出自 码同学软件测试 码同学公众号 自动化软件测试 码同学抖音号 小码哥聊软件测试 Hello 大家好 今天小编给大家分享一个实现mock服务的工具moco 那么问题来了 什么是moc
  • 浅谈软件危机

    什么是软件危机 软件危机是计算机软件在它的开发和维护过程中所遇到的一系列严重问题 概括地说 主要包含两方面的问题 如何开发软件 怎样满足对软件日益增长的需求 如何维护数量不断膨胀的已有软件 危机实例 IBM OS 360 操作系统被认为是一
  • C++与Java比较(转)

    作为一名C 程序员 我们早已掌握了面向对象程序设计的基本概念 而且Java的语法无疑是非常熟悉的 事实上 Java本来就是从C 衍生出来的 然而 C 和Java之间仍存在一些显著的差异 可以这样说 这些差异代表着技术的极大进步 一旦我们弄清
  • VS2019 windows驱动开发环境配置

    配置Windows驱动开发环境 VS2019 下载VS2019 下载链接 https visualstudio microsoft com zh hans downloads 选择你所需要的开发环境和配置 确认下载完后 在你安装的WDK 的
  • 【数据库系统概论】第三章:SQL

    B站视频 B站第一章 参考资料 图片来自视频链接和参考资料 本章目录 SQL特点 SQL基本概念 SQL的基本语法 SQL特点 SQL包括 数据查询 数据操作 数据定义 数据控制 它是一个非过程语言 什么是非过程性语言 我们在逻辑和物理模型
  • WPF依赖属性优先级

    有下面代码 属性 样式 触发器都设置了按钮的颜色 请问按钮最终是什么颜色