[英]How can I make Prism's DelegateCommand observe a child view-model's property?
My "parent" view-model ( AnalyzeVm
) has a child view-model ( ScanVm
) that represents something that can be saved to disk.我的“父”视图模型 ( AnalyzeVm
) 有一个子视图模型 ( ScanVm
),它表示可以保存到磁盘的内容。 I want to give the parent a command ( SaveCmd
) that is enabled only when the ScanVm.IsDirty
property is true.我想给父级一个命令 ( SaveCmd
),该命令仅在ScanVm.IsDirty
属性为真时才启用。
I already use DelegateCommand
and its ObservesProperty
functions all over the place so I tried to use it for this.我已经在各处使用了DelegateCommand
及其ObservesProperty
函数,因此我尝试将其用于此目的。 But in this one case I cannot get the ObservesProperty
expression to trigger a call to CanExecute
.但在这种情况下,我无法获得ObservesProperty
表达式来触发对CanExecute
的调用。
The difference here (from all the other places I use ObservesProperty
that work fine) is that I'm monitoring a property of the child object. Not of my own这里的区别(与我使用ObservesProperty
的所有其他地方工作正常)是我正在监视孩子 object 的财产。不是我自己的
I need to understand why this is not working.我需要了解为什么这不起作用。 I thought this was a valid thing to do.我认为这是一件有效的事情。
Here are the two view models (stripped down for clarity)这是两个视图模型(为清楚起见被剥离)
// Parent view-model. Derives from a class that implements INotifyPropertyChanged.
public class AnalyzeVm : BaseViewModel
{
private ScanVm _scan;
public AnalyzeVm(ScanVm scan)
{
_scan = scan;
// Set up the command. Try to make it monitor a property of the ScanVm
// object instead of one of our own.
SaveScanCmd = new DelegateCommand(
() => { _scan.Save() } // execute -- saves the scan
() => { return _scan.IsDirty; }) // only valid when scan is dirty
.ObservesProperty(() => scan.IsDirty); // So observe IsDirty property
// Now just as a sanity check, add a handler for that property changing
// to the PropertyChangedEventManager and log when it does.
PropertyChangedEventManager.AddHandler(
scan,
(_, _) => Debug.WriteLine("IsDirty changed"),
nameof(ScanContext.IsDirty));
}
public ICommand SaveScanCmd { get; }
// other code...
}
// ScanVm class.
public class ScanVm : BaseViewModel
{
private bool _isDirty;
public bool IsDirty
{
get => _isDirty;
set => SetProperty(ref _isDirty, value); // Raises PropertyChanged event
}
public void Save()
{
// Code here to save the ScanVm
IsDirty = false; // No longer dirty
}
// ... other code
}
I am sure that the IsDirty
property is properly changing and firing the event.我确信IsDirty
属性正在正确更改并触发事件。 I confirmed it many times.我确认了很多次。 And I'm already using Observes
property successfully when I observe my own class' properties.当我观察我自己的类的属性时,我已经成功地使用了Observes
属性。 For example, I were to, take the SaveCmd
and move it into the ScanVm
class itself -- so that the property expression just observed the ScanVm
's own property , then it works fine.例如,我SaveCmd
ScanVm
class 本身——这样属性表达式就观察到ScanVm
自己的属性,然后它就可以正常工作了。 I do that all over the place in my code.我在我的代码中到处都是这样做的。
This is the only place I'm trying to observe another object's property.这是我试图观察另一个对象的属性的唯一地方。 Is it valid to do?这样做有效吗? if so, what am I doing wrong?如果是这样,我做错了什么?
ObservesProperty
primarily works on properties of the containing object ObservesProperty
主要作用于包含 object 的属性
public bool IsDirty {...}
...ObservesProperty( () => IsDirty );
plus nested properties加上嵌套属性
public ScanVM Child {...}
...ObservesProperty( () => Child.IsDirty );
It just doesn't observe properties on any given instance, because it starts looking from the containing object ( AnalyzeVm
in this case).它只是不观察任何给定实例的属性,因为它从包含AnalyzeVm
(在本例中为 AnalyzeVm)开始查找。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.