I have to migrate an old Delphi application to WPF with the MVVM Light framework, in my main screen I have about 50 MenuItem
s... (no comment).
Currently, each MenuItem
has a RelayCommand
that executes a generic method based on a precise model:
<MenuItem Command="{Binding ShowOrderCommand}"/>
and in ViewModel
ShowOrderCommand = new RelayCommand(ShowGridType<OrderModel>, CanShowGridType)
(where OrderModel
interface is IBaseModel
) calling this method definition:
ShowGridType<T>() where T : IBaseModel
(note: generic type must be kept because of DI resolution further in code).
I would like to remove these hundreds of lines of RelayCommand<OrderModel, PriceModel, ...>
instantiations by a single command that can pass the type of model via a command parameter (or other) with a generic method like ShowCommand = RelayCommand<IBaseModel>
or RelayCommand<TModel>
.
I thought I had found the solution simply through this:
<MenuItem Command="{Binding ShowCommand}" CommandParameter="{x:Type models:OrderModel}"/>
ShowCommand = RelayCommand<IBaseModel>(ShowGridType);
ShowGridType<Tmodel>(Tmodel model) where Tmodel : IBaseModel
But when I click on my MenuItem I have a conversion error:
Unable to cast object of type 'System.RuntimeType' to type 'XXXXXX.Models.IBaseModel'
I have also tried to receive an 'object' instead of a IBaseModel
, but can't find how to use this with the generic definition ShowGridType<T>
.
Any idea that will help?