Reusable toolbar

Topics: Prism v2 - WPF 3.5
Sep 17, 2009 at 4:13 PM

Hi, I am developing a composite application using Prism.  A requirement is to have a series of tabs each one editing a different business object.  For example, tab 1 will insert/modify/delete Customers, tab 2 will insert/modify/delete Providers, tab 3 ... Orders... and so on.  So basically the toolbar will have a New, Refresh, Edit, Save, Delete, Close commands.  Several Modules will also use this toolbar.  

  My question is:  how do I implement this toolbar?  In the shell? In a separate module?  





Sep 17, 2009 at 5:58 PM

Hi Guillermo,

My vote would be to place them in a separate module.  If you have questions on how this can be done the following blog has a loosely related example (separate module for control):


Sep 18, 2009 at 8:53 AM
Edited Sep 18, 2009 at 12:51 PM

Hi, Billkrat.

Thanks for your answer.

This is what I have done.  I don't know if it is a right approach. 

I have a Module.Common library project that contains common classes for all modules.  I have declared a IToolbarService, ToolbarService, ToolbarView, and a ToolbarItem.  The ToolbarView is a usercontrol that contains basically:


 <ToolBar  HorizontalAlignment="Stretch"
                                  ItemTemplate="{DynamicResource toolbarItemTemplate}"


and its ItemTemplate:


<DataTemplate x:Key="toolbarItemTemplate">
  <Button   Command="{Binding Command}"
               Content="{Binding Header}" />

ToolbarItem is the object that will hold the Header and Command (of type DelegateCommand<string>)  properties, and implements INotifyPropertyChanged. 



 public interface IToolbarService : System.ComponentModel.INotifyPropertyChanged

        ObservableCollection<ToolbarItem> ItemsList
        void AddItem(string header, Action<string> executeMethod, Func<string, bool> canExecuteMethod);


ToolbarService implements IToolbarService.  The AddItem implementation:


     DelegateCommand<string> command = new DelegateCommand<string>(executeMethod, canExecuteMethod);
     ItemsList.Add(new ToolbarItem(header, command));

I have referenced the Module.Common project in every other module. In every ModuleInit, I call _container.RegisterType<IToolbarService, ToolbarService>() in the Initialize method (I am not sure if every module needs to register the service or just the shell). In the views that show the toolbar I add a ToolbarView usercontrol like this:


   <common:ToolbarView  DataContext="{Binding ToolbarItems}" />

In the ViewModel of each View (or in a ViewModelBase) I declare a ToolbarItems property of the same type as in IToolbarService. In the constructor of the viewmodel, I receive the IToolbarService by injection. So, I can call :

       toolbarService.AddItem("Refresh", ExecuteRefresh, CanExecuteRefresh);
       ToolbarItems = toolbarService.Items;

Voila! The toolbaritems are shown in the view.

So, that is what I got. Please, let me know if it is ok.




Sep 18, 2009 at 12:30 PM

Very nice Guillermo, looks like a great blog entry - short, simple and reusable.   If you don't have a blog I might recommend getting one; I learned it simplifies life when later you find someone looking to do something you previously blogged about.   Thank you for sharing your code!

Sep 18, 2009 at 7:47 PM


  It we be awesome if you could throw together a simple example app.  Thanks!