I have a wpf application that has a TreeView in it. The TreeView has a context menu. The context menu only starts loading after I right click on one of the TreeViewItems. (How I know this - I added a log to the context menu's loaded event. Once I clicked on one of the TreeViewItems it wrote the log message. The problem this causes is that it seems as if the context menu doesnt work the first time its being clicked. When actually the load just takes time the first time.) Since it takes a minute or two to load, I want it to start loading immediately once the application is up. The context menu doesnt have a "Load" action. Any ideas how to make this happen?
The context menu items was binded to an inner property in the data context (for example if I had a property named Manager and it had a property named MenuItems, it was binded to Manager.MenuItems). The inner property only had a get property that when calling it, it would calculate the observable collection of menu items. So What I did to resolve my problem I signed the TreeView's event of Loaded and there I ran over all the first hierarchy TreeViewItems, for each one I did:
item.IsSelected = true;
item.Focus();
item.IsOpen = true;
var items = Manager.MenuItems; // Calculates the menu items (it's per item in my code so it doesn't look exactly the same
The fourth line was to calculate the menu items before load. The 2 last lines must come together, for some reason it doesn't work if one isn't there. Hope this helps someone