加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

wpf – 依赖属性SetValue()和SetCurrentValue()之间的区别是什么

发布时间:2020-12-14 01:08:24 所属栏目:百科 来源:网络整理
导读:我问这个问题的原因是因为我被@Greg D(从 this question)推荐使用SetCurrentValue(),而是看看文档,没有看到什么区别。或者什么“不改变其价值源”是什么意思? SetValue() 设置依赖属性的本地值,由其依赖属性标识符指定。 SetCurrentValue() 设置依赖属性
我问这个问题的原因是因为我被@Greg D(从 this question)推荐使用SetCurrentValue(),而是看看文档,没有看到什么区别。或者什么“不改变其价值源”是什么意思?

SetValue()

设置依赖属性的本地值,由其依赖属性标识符指定。

SetCurrentValue()

设置依赖属性的值,而不更改其值源。

你提供的MSDN链接说得很好:

This method is used by a component
that programmatically sets the value
of one of its own properties without
disabling an application’s declared
use of the property. The
SetCurrentValue method changes the
effective value of the property,but
existing triggers,data bindings,and
styles will continue to work.

假设你正在编写TextBox控件,并且你暴露了一个人们经常使用的Text属性,如下所示:

<TextBox Text="{Binding SomeProperty}"/>

在控件的代码中,如果调用SetValue,您将使用提供的任何内容覆盖绑定。如果你调用SetCurrentValue,但是,将确保该属性接受给定的值,但不会销毁任何绑定。

据我所知,Greg的建议是不正确的。你应该总是使用GetValue / SetValue从你的CLR包装器属性。 SetCurrentValue在您需要属性来接受给定值但不想覆盖针对您的属性配置的任何绑定,触发器或样式的情况下更有用。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读