Can I suppress the “CanExecute” call within the MV

2019-09-06 12:28发布

问题:

I am working with a tree structure using WPF and the MVVM pattern. I start out by creating 20 root nodes and lazy loading the child nodes as each node is clicked. So for instance ... if I have the following:

Level 1
    Level 1.1
Level 2
Level 3
    Level 3.1
    Level 3.2

Levels 1, 2 and 3 are loaded at run time. Levels 1.1, 3.1 and 3.2 would not be loaded until their respective parents are clicked. Levels below 1.1 etc. are loaded the same way, by clicking on their parent.

My issue is when I click on Level 1.1 to load its children, the "CanExecute" method is checked for Level 1.1 AND all root level items for each child of Level 1.1. This causes quite a bit of wasted time if there are numerous children.

My question is, can I somehow suppress the call to "CanExecute"? I have no need to call it as these sub-levels and I'd like to bypass it. I am very new to the MVVM framework so I'm not sure if this is even possible.

回答1:

If you use Prism(Composite Application Guidance for WPF and SilverLight) 'DelegateCommand' in your view model, you will have to explicitly call command.RaiseCanExecuteChanged whenever you want CanExecute to be called on it.