Joh*_*son 5 wpf binding dependency-properties
我有时想知道依赖属性的值是来自用户输入还是来自绑定源的更改。我还没有找到一个干净的方法来确定这一点。
有像DependencyPropertyHelper这样的东西,但据我所知,它对这种情况没有帮助。
场景:<TextBox Text="{Binding Foo}" />
查明最后更新的是绑定源还是目标Text
。或者其他什么,是的,我知道触发器、继承动画等。
是的,您可以获取与依赖属性关联的绑定表达式并检查其状态:
BindingOperations.GetBindingExpressionBase(textBox, TextBox.TextProperty)?.Status ==
BindingStatus.Active
Run Code Online (Sandbox Code Playgroud)
您可以将其与 结合起来DependencyPropertyHelper
检查当前源是否为BaseValueSource.Local
.
ValueSource
还有一个名为 的属性,在使用绑定或任何其他表达式(例如或 )时IsExpression
设置为。true
DynamicResource
TemplateBinding
确定当前值是来自源还是目标更困难。AFAIK 没有比这更好的方法了:
<TextBox Text="{Binding Path=Foo, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"
SourceUpdated="OnSourceUpdated" TargetUpdated="OnTargetUpdated" />
Run Code Online (Sandbox Code Playgroud)
然后您可以连接处理程序OnSourceUpdated
并OnTargetUpdated
应用一些逻辑。您还可以创建附加属性并更新它以获得更好的封装。