简体   繁体   English

如何让 Prism 的 DelegateCommand 观察子视图模型的属性?

[英]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.

相关问题 如何使用Style将子UserControl的Dependency属性绑定到宿主元素的视图模型的属性? - How to bind a child `UserControl`'s Dependency Property, using `Style`, to a property of the host element's view-model? Prism中的DelegateCommand,其canExecuteMethod由ObservableCollection中的属性确定。 我如何继续“观察” canExecute? - DelegateCommand in Prism whose canExecuteMethod is determined by a property in an ObservableCollection. How do I continue to “Observe” canExecute? Prism的DelegateCommand内存泄漏 - Prism's DelegateCommand memory leak 如果usercontrol的datacontext是视图模型,您如何绑定到xaml代码后面的属性? - How do you bind to a property in a xaml code-behind if the usercontrol's datacontext is a view-model? 如何在需要时创建棱镜的视图模型? - How to create view-model of prism when needed? 如何在视图模型的属性更改时更新子窗口? - How to update Sub Window on view-model's properties changed? 棱镜:ViewModelLocator创建视图模型实例,但是我该如何定位呢? - Prism: ViewModelLocator creates the view model instance(s) but how can I target it? 如何在更改子视图模型属性时通知父视图模型? - How to notify parent's view model when child's view model property is changed? 棱镜如何找到在views文件夹的子文件夹中的视图 - prism how to find the view which in the views folder's child folder 如何从子视图访问模型属性的名称 - How do I get access to the model property's name from a child view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM