XAML 绑定自动实现的属性?
是否可以使用 XAML 数据绑定到具有自动实现属性的模型?
class ClassA
{
// pseudo code.
int Width { get; set{ NotifyPropertyChange("Width");} }
}
//XAML
<textBox width="{Binding Path=Width,Mode=OneWay}"/>
Is it possible to using XAML data binding to a model with Auto-Implemented properties?
class ClassA
{
// pseudo code.
int Width { get; set{ NotifyPropertyChange("Width");} }
}
//XAML
<textBox width="{Binding Path=Width,Mode=OneWay}"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自动属性没有半自动模式。它需要要么是一个没有任何额外内容的自动属性:
要么是一个带有后备存储的完全扩展的属性,可以添加额外的代码,例如更改通知:
如果您使用自动属性,您仍然可以绑定到它们,但您'放弃更改通知,因此您通过代码对属性所做的任何更改都不会显示在 UI 中。一般来说,用于数据绑定的任何对象都应包含更改通知,因此不应使用自动属性。
Auto-properties don't have a half auto mode. It needs to either be an auto-property with nothing extra:
or a fully expanded property with a backing store that can have additional code added to it, like change notification:
If you use auto-properties you can still bind to them but you're giving up change notification, so any changes you make to the property from code won't show up in the UI. In general any object being used for data binding should include change notification and so should not use auto-properties.
我这样解释自动财产。
是单向绑定到视图始终是可能的。
仅当您希望一个模型元素中的更改导致 gui 或其他观察者自动更新时,双向绑定才需要
NotifyPropertyChange("propertyname");
。有一个工具可以为您自动生成 INotifyPropertyChange 实现:notifypropertyweaver。
更新
code-magazine 文章中描述了两种方式绑定的 INotifyPropertyChange-Free 替代方案“INotifyPropertyChanged 已过时” 使用免费库 codeplex 上的 updatecontrols
i interprete Autoproperty this way.
Yes one-way binding to view is always possible.
Twoway binding require
NotifyPropertyChange("propertyname");
only if you want that changes in one modell element cause automatic update of the gui or other observers.There is tool that can autogeneretates INotifyPropertyChange implementation for you: notifypropertyweaver.
Update
There is also a INotifyPropertyChange-Free altenative for two way binding described in code-magazine article "INotifyPropertyChanged Is Obsolete" using the free lib updatecontrols on codeplex
您的示例不是自动实现的属性,事实上它可能无法编译。
要实现此目的,您需要实现完整的后备存储属性或使用方面来实现 INotifyPropertyChanged,以便您可以保持 Auto 属性干净。
Your example is not a Auto-implemented property, in fact it will probably not compile.
To implement this you would need to either implement a full backing-store property or use aspects to implement the INotifyPropertyChanged so that you can keep your Auto property clean.
是的,您可以使用具有 Auto 属性的 xaml 绑定。但如前所述,所示财产不是汽车财产。
Yes you can use xaml binding with Auto properties. But as said, the property illustrated is not an Auto property.