Activating / deactivating toolbar buttons with Prism

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?

+4
source share
2 answers

I would not insert Button instances in the ObservableCollection . Think about it instead:

Create ViewModel for toolbar buttons

 class ToolBarButtonViewModel : INotifyPropertyChanged { // INotifyPropertyChanged implementation to be provided by you public string ImageSource { get; set; } public ICommand Command { get; set; } public bool IsVisible { get; set; } } 

Then, of course, change the type of ToolBarItems to their collection.

In the ShellView add a DataTemplate for the ToolBarButtonViewModel and bind the ItemsSource to any toolbar control to the ViewModels collection, for example:

 <DataTemplate> <Button Command="{Binding Command}"> <Button.Content> <Image Source="{Binding ImageSource}" /> </Button.Content> </Button> </DataTemplate> 

Now you can bind Button.Visibility to IsVisible using the BooleanToVisibilityConverter to solve your immediate problem.

As an added bonus, you can also:

  • Change the appearance of toolbar buttons completely from XAML
  • Bind any property of the visual tree for the toolbar button to the corresponding properties on the ToolBarButtonViewModel

Update

The mechanism for turning on / off the buttons depends on the features of your application. There are many options - here are a few (keep this chart in mind when reading):

+4
source

Here is an article on this - this should help with your questions. It includes modules that load custom task buttons and Ribbon tabs.

0
source

Source: https://habr.com/ru/post/1345576/


All Articles