I have a following problem. I have some items in my treeview which are at the top of hierarchy. When I click on some item to expand it, it can event take 5 seconds to load all items in selected node. How can show “Please wait…expanding node..” message during this process? How can I determine whether item is still expanding or all nested items are loaded and presented now?
I have already asked very similar question here Show "Please wait.." message when expanding TreeView in WPF, I even mark the answer as accepted but however, the solution I have now is not satisfactory because “Please wait..” dialog is blinking (it’s shown and closed as many times as number of nested items).
As I’ve mentioned in comment to your post, two problems may exists we need to solve.
WPF doesn’t provide BeforeExpand event, so you should use view data, notifications on property changed, data binding and observable collections. I’ll illustrate this in simple application.
The whole approach can be approximately described as:
I’ve designed my tree item ViewData class with observable properties IsExpanded, IsLoaded, observable collection of children items and Name that will be displayed:
Next step consists in developing Presentation Model. As you can see our model listens to IsExpanded PropertyChanged event and when it equals to true start LoadingInnerItems.:
XAML for our TreeView. When we are loading inner items – foreground of our treeviewitem goes red:
And don’t forget set model as datacontext of view in code behind: