gpt4 book ai didi

c# - 另一个 View 模型中的 RaisePropertyChanged

转载 作者:行者123 更新时间:2023-11-30 20:42:11 26 4
gpt4 key购买 nike

我有一个主 ViewModel

public class MainViewModel : ViewModelBase
{
public MenuViewModel MenuVM { get; set; }
public StatusBarViewModel StatusBarVM {get; set; }
}

每个 subview 模型都有绑定(bind)到 View 上的属性:

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。

所以问题是当我更改 Property2 时,Property1.Get 没有被调用(我使用断点进行了测试)。

问题是:

为什么这不起作用? 如何使这项工作?

谢谢 :)

最佳答案

记住!您不能从实例调用 RaisePropertyChanged(),因为该方法在 MVVM Light 中受到保护!因此,在您的 MenuViewModel 中创建一个包装器:

public void RaiseProperty1Changed()
{
RaisePropertyChanged("Property1");
}

在您的 MainViewModel 中订阅 StatusBarViewModelProperty2 的事件 RaisePropertyChanged

StatusBarVM.PropertyChanged += OnProperty2Changed

并且在这个委托(delegate)方法调用中:

private void OnProperty2Changed(object sender, PopertyChangedEventArgs e)
{
if (e.PropertyName == "Property2")
{
MenuVM.RaiseProperty1Changed();
}
}

关于c# - 另一个 View 模型中的 RaisePropertyChanged,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31580953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com