-1

I’m using the WPF RibbonMenu which will hold buttons to launch multiple tools. Probably 50 or more. When a tool is selected from the RibbonMenu, it creates a new tab in my AvalonDock layout.

I’m using a ViewModel-First MVVM pattern and the way I have it working at the moment is each tool ViewModel (I have only added two so far) is injected into the RibbonMenu ViewModel so it can be added to an ObservableCollection which is bound to the AvalonDock Manager.

This works great, but it will mean injecting every tool ViewModel (50+) into the Ribbon Menu ViewModel which doesn't seem right. I assume each injection will use memory regardless of whether it is used or not by the user?

Similar posts like How to avoid Dependency Injection constructor madness? have answers that say a large number of injections in the constructor means that the class is doing too much and needs to be broken up.

But how is this achieved in my situation where I have a need a single ViewModel for my RibbonMenu?

EDIT

I'm trying to decide whether to use IoC in the RibbonMenu ViewModel and have lots of injections (50+), or ignore IoC for this ViewModel only and just create new dependent instances within the ViewModel. For example, each button would trigger a method containing:

HomeViewModel homeViewModel = new HomeViewModel();

Any advice on the best way forward?

Richard
  • 439
  • 3
  • 25

1 Answers1

0

I can say I have not personally tried using a RibbonMenu but can you inject a list of ViewModels into the constructor? Something like

//Initialize ObservableCollection
public ObservableCollection<IRibbonItemViewModel> RibbonItems = new ObservableCollection<IRibbonItemViewModel>();

//Constructor<>
HomeViewModel homeViewModel = new HomeViewModel(List<IRibbonItemViewModel> ribbonViewModels)
{
   foreach(var vm in ribbonViewModels)
   {
       RibbonItems.Add(vm);
   }
}

Make sure all ViewModels implement interface IRibbonItemViewModel which could be as simple as

public interface IRibbonItemViewModel {}
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
ivie
  • 61
  • 3