tags:

views:

328

answers:

1

I'm looking for a smooth method of managing toolbars (and menus) with mvvm in WPF.

Consider an UI with tabbed workspaces and heterogenous content (like Visual Studio). There the toolbars should be hidden or visible depending on the active tab. How would you design the view viewmodel for the toolbars?

I'd use a collection of toolbar-viewmodels and bind the ToolbarTray to it, but afaik that's not possible.

Any recommendations are apreciated. Links to samples, best practice papers, etc. are welcome.

+1  A: 

I had this same issue in one of my applications recently. I was using a Ribbon as my main toolbar, and based on what module was selected the buttons on the ribbon had to be hidden or shown. I came up with two different solutions... hopefully one will work for you.

The first solution would be to create a region in the shell of your application (it can be a Grid/Canvas/whatever) and dynamically load the desired toolbar into this region based on what tab the user clicks on. In other words, if you have 5 different tabs that require their own toolbar, create 5 UserControls that contain a toolbar and load the correct one into the region when the user clicks on the tab. Next you'll want to respond to the event of when the user clicks on a button on the toolbar. In my solution I used the Mediator approach to allow ViewModels to communicate with each other. It works, but I don't know that I would do it again that way... CAL is probably the better approach.

The other solution would be to create one toolbar with all of the required buttons and bind their visibility to the ViewModel. Based on the user's selection, set the visibility of the controls to whatever your requirements may be. Hopefully one of these solutions works for you.

by the way... what part of Germany are you from? I know a man with the last name of Stoll, so I wasn't sure if it was a common name or not...

Brent
I did something similar to your first solution, but using a single UserControl. Primarily I tried to use DataTemplates with DataType for selecting the ToolBar View, but didn't solve this, so I ended up using the same UserControl for every Toolbar. The Items are data bound. So this works great, but is not as flexible as your solution.In respect of the visibility problem I used a similar aproach. This can easyly be encapsulared into a class with Merge/Unmerge methods.I'm from Baden-Würtemberg. But the name came from Thüringen, some generations ago.
Michael Stoll
Hmm... I worked in Donaueschingen for 4.5 weeks with a man named Wolfgang Stoll... I think he lived close to Triberg, but I can't remember exactly.
Brent