Im in the process of exploring the framework of Prism and Ive already gone. But I was wondering how to create toolbars (and context menus) where each module can register its own buttons.
In this example, I want all the buttons to be in the same ToolBar that is in my shell. ToolBars ItemsSource bound to the ToolBarItems property of type ObservableCollection<FrameworkElement> in the view model. Elements can be added to this collection using the ToolBarRegistry service. This is the ViewModel:
public class ShellViewModel { private IToolBarRegistry _toolBarRegistry; private ObservableCollection<FrameworkElement> _toolBarItems; public ShellViewModel() { _toolBarItems = new ObservableCollection<FrameworkElement>(); _toolBarRegistry = new ToolBarRegistry(this); } public ObservableCollection<FrameworkElement> ToolBarItems { get { return _toolBarItems; } } }
Note that the collection of type FrameworkElement will be reorganized as a more specific type if this proves to be the right solution.
My ToolBarRegistry has a way to register image buttons:
public void RegisterImageButton(string imageSource, ICommand command) { var icon = new BitmapImage(new Uri(imageSource)); var img = new Image(); img.Source = icon; img.Width = 16; var btn = new Button(); btn.Content = img; btn.Command = command; _shellViewModel.ToolBarItems.Add(btn); }
I call this method from my OrderModule and the buttons are displayed correctly. So far so good.
The problem is how can I control when these buttons should be removed again. If I go to the view in another module (and sometimes another view in the same module), I want these buttons specific to the module to be hidden again.
Do you have any suggestions on how to do this? Am I approaching this problem incorrectly, or can I change what I already have? How did you solve this problem?
source share