views:

60

answers:

2

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  A: 

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.

NVM
@NVM Unfortunately we're not using Prism. We're using the Unity framework. I should have clarified that in my question.
Scott Vercuski
You don't need to use Prism to use DelegateCommand. You can just yank it from the source code, or any other implementation of DelegateCommand on the interwebs. It is a VERY simple class.That being said, this isn't enough to solve your "problem". This is because the "CanExecute" is being called by WPF regardless of the CanExecuteChanged event. I put "problem" in quotes, because I question if it is really a problem. If your implementation of CanExecute is fast and small (it should be), it shouldn't matter. What problem are you trying to solve by suppressing the call into CanExecute?
Brian Genisio
Brian is right in that you dont need to use Prism to use DelegateCommand. But yanking things out of a library and using wherever you want is almost always a maintainence problem.That said I take my statement that you will have to always explicitly call RaiseCanExecuteChanged for the CanExecute to be called, back as incorrect. My prism app is structured in a way that this happens but its not necessarily always true.
NVM
@Brian and NVM Thank you for the replies !! my issue is that the tree is loading extremely slow for a large amount of data. I've check the SQL call and object creation and they run in a short amount of time. In debugging I went line by line and noticed all of the "CanExecute" function calls each time a new tree item was added. Since the method that does the SQL call and initial object creation finishes quickly the only thing left in the debugging calls are the multiple "CanExecute" calls.
Scott Vercuski
One thing that you can try is using UI virtualization in the tree view. <TreeView VirtualizingStackPanel.IsVirtualizing="True" … />What exactly are you doing in CanExcute?
NVM
A: 

use e.handeled = true in the routedcommand handler.

saurabh