Caliburn - 子 ViewModel 的 PropertyChanged
我在 WPF 应用程序中使用 Caliburn 和 MVVM 模式,并尝试使用尽可能多的约定。我遇到的一个问题是当我必须在导入的类上连接一些属性更改通知时。
假设我有一个像这样的 ViewModel:
class ViewModelA
{
private readonly IViewModelB _b;
public ViewModelA(IViewModelB b)
{
_b = b;
}
public bool CanGo
{
get { return _b.MyBoolProperty; }
}
public void Go()
{
//Does something here
}
}
当 ViewModelB 触发 MyBoolProperty 的 PropertyChanged 事件时,调用 NotifyOfPropertyChange(() => CanGo) 的推荐(正确)方法是什么?
过去我使用 PropertyObserver 类型类来管理它。
还是我设计的这个场景完全错误?
I'm using Caliburn and the MVVM pattern in a WPF application, and am trying to use as many conventions as possible. One issue I'm running into is when I have to wire-up some property-changed notifications on an imported class.
Say I have a ViewModel like so:
class ViewModelA
{
private readonly IViewModelB _b;
public ViewModelA(IViewModelB b)
{
_b = b;
}
public bool CanGo
{
get { return _b.MyBoolProperty; }
}
public void Go()
{
//Does something here
}
}
What is the recommended (correct) way to call NotifyOfPropertyChange(() => CanGo) when the PropertyChanged event for MyBoolProperty is fired off ViewModelB?
In the past I've used a PropertyObserver type class to manage this.
Or am I designing this scenario completely wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的“子”模型通过公共属性公开,您可以使用 DependencyAttribute 来跟踪更改:
为了正常工作,整个属性路径应该由通知对象组成。
您还可以在最后加上“*”
以指示 B 的所有属性都应导致前置条件重新评估;请注意,“*”仅作用于属性路径的末尾,并且仅作用于一个深度(它不跟踪 B 的子模型上的更改)。
If your "sub"-model is exposed with a public property, you could use DependenciesAttribute to track changes:
To work properly, the whole property path should be composed of notifying objects.
You can also put a final "*"
to indicate that all properties of B should cause the precondition re-evaluation; note that "*" only acts on the end of the proprerty path and just for one level of depth (it doesn't track changes on sub-models of B).