DependencyProperty和DataBinding? [英] DependencyProperty and DataBinding?

查看:104
本文介绍了DependencyProperty和DataBinding?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中:

有人可以解释DependencyProperty和Databinding之间的关系吗?

Can someone please explain the relationship between DependencyProperty and Databinding?

我有一个财产在我的代码背后我想成为我的数据绑定的来源。
如果我想将此对象绑定到XAML上的文本框,DependencyProperty(或它)是否会发挥作用。

I have a property in my code behind I want to be the source of my databinding. When does a DependencyProperty (or does it) come into play if I want to bind this object to textboxes on the XAML.

推荐答案

绑定中的目标必须始终为 DependencyProperty ,但任何属性(甚至普通属性)都可以作为源。

The target in a binding must always be a DependencyProperty, but any property (even plain properties) can be the source.

普通属性的问题是绑定只会接受一次值,之后不会更改,因为从源代码属性中删除更改通知。

The problem with plain properties is that the binding will only pick up the value once and it won't change after that because change notification is missing from the plain source property.

要提供变更通知而不使其成为 DependencyProperty ,可以:

To provide that change notification without making it a DependencyProperty, one can:


  1. 在定义属性的类上实现 INotifyPropertyChanged

创建一个 PropertyName 更改事件。 (向后兼容性)

Create a PropertyNameChanged event. (Backward compatibility.)

WPF将会更好地与第一选择。

WPF will work better with the first choice.

这篇关于DependencyProperty和DataBinding?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆