【发布时间】:2015-10-13 09:07:48
【问题描述】:
我有一个主视图模型
public class MainViewModel : ViewModelBase
{
public MenuViewModel MenuVM { get; set; }
public StatusBarViewModel StatusBarVM {get; set; }
}
每个子视图模型都有绑定在视图上的属性:
public class MenuViewModel
{
private string _property1;
public string Property1
{
get { return _property1; }
}
}
和
public class StatusBarViewModel
{
private string _property2;
public string Property2
{
get { return _property2; }
set
{
_property2 = value;
RaisePropertyChanged("Property2");
RaisePropertyChanged("Property1");
}
}
}
我想做的是,什么时候Property2被改变了,为了更新Property1,raise property改变。
所以问题是 Property1.Get 在我更改 Property2 时没有被调用(我用断点进行了测试)。
问题是:
为什么这不起作用? 如何做这项工作?
谢谢:)
【问题讨论】:
-
您需要为正确的视图模型实例调用它。例如。
menuViewModelInstance.RaisePropertyChanged("Property1"); -
@Sinatr,它不能工作,因为从属性的另一个实例调用 RaisePropertyChanged 时 MVVMLight 处理程序为空。然后它返回而不调用处理程序。见代码: var handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); }