我的WPF应用程序中有一个属性,我试图绑定到UI。当我直接在viewmodel中更新属性时,它按照我的预期工作。但是,当我尝试使用Event从另一个类内部更新此属性时,绑定似乎不起作用。仔细观察后,我认为这种情况正在发生,因为当发生这种情况时,PropertyChangedEventHandler为空。
我在ViewModel中的属性:
public int BeatNumber
{
get
{
return beatNumber;
}
set
{
if (beatNumber != value)
{
this.beatNumber = value;
RaisePropertyChanged(() => BeatNumber);
}
}
}
ViewModel中的事件:
public event EventHandler GetHtmlDone = delegate { };
public void GetHTML(string url)
{
BeatNumber++;
}
通知对象:
public class NotificationObject : INotifyPropertyChanged
{
protected void RaisePropertyChanged<T>(Expression<Func<T>> action)
{
var propertyName = GetPropertyName(action);
RaisePropertyChanged(propertyName);
}
private static string GetPropertyName<T>(Expression<Func<T>> action)
{
var expression = (MemberExpression)action.Body;
var propertyName = expression.Member.Name;
return propertyName;
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
如何在从其他类调用我的事件时更新此绑定?