[英]WPF only Binding to auto-implemented Property
I have a label that binds to a property, the binding only works when the property in the view model is auto-implemented. 我有一个绑定到属性的标签,绑定仅在视图模型中的属性自动实现时才有效。
In XAML: 在XAML中:
<Label Content="{Binding MyProperty}" />
In ViewModel: 在ViewModel中:
public virtual string MyProperty { get; set; }
//This code above works fine, but when i use it as below,binding doesn't work
private string _myProperty;
public virtual string MyProperty
{
get { return _myProperty; }
set { _myProperty = value; }
}
You're probably using a compile-time code generation utility to handle INotifyPropertyChanged.PropertyChanged
for you, such as Fody or DevExpress. 您可能正在使用编译时代码生成实用程序来为您处理INotifyPropertyChanged.PropertyChanged
,例如Fody或DevExpress。 Such tools would handle an auto-implemented property but you'd need to handle your fully-implemented properties by explicitly raising the event after you change property value. 此类工具将处理自动实现的属性,但您需要通过在更改属性值后显式引发事件来处理完全实现的属性。
private string _myProperty;
public virtual string MyProperty
{
get { return _myProperty; }
set
{
_myProperty = value;
OnPropertyChanged(nameof(MyProperty)); // or however your base view-model class method signature for raising the event
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.