Hi:
I'm using the Telerik CUSTOM_MVVM_WPF example to build out a series of menus using
MVVM and RadMenuItems.
I'm having difficulty building out the MVVM so that I can pass parameters to the
commands associated with the individual menu items.
How d I extend the implementation to achieve this?
Thanks,
JohnB
5 Answers, 1 is accepted
You could add a Command inside the MenuItemViewModel and bind it to the Command property of the MenuItem. After that you need to implement some custom logic for the specific items inside the Action that is executed when the command is invoked. As for the parameter you could easily pass it to the Action using the CommandParameter property of the RadMenuItem.
We have modified the sample in order to demonstrate you the described above approach.
If you want to pass some specific parameter for each item you could also add property for the parameter inside the MenuItemViewModel and bind it to the CommandParameter of the RadMenuItem.
We hope this will help you.
Regards,
Nasko
Telerik by Progress
Nasko: Is there an issue with the zipped project, or am I missing something.
The archive seems to be empty of .cs and .xaml files.
Thanks,
JB
We have noticed that you are having troubles with the unzipping of the exact same project attached in this forum thread. As your issue is related with this thread as well, please check the answer we have provided in the other forum thread.
Hope this helps.
Regards,
Nasko
Telerik by Progress
Hi Nasko:
This is a good start, but could you further extend this so:
- I can see the parameter being set on a specific menu item
- see how to consume the parameter in a custom command?
Consider the following custom command I have attached to a menu:
using System;
using System.Windows;
using System.Windows.Input;
public class ApplicationMaximizeCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
//MessageBox.Show("Application Shutdown Command Clicked!!!");
// For example with this instance I would like to pull the
// Main Application window from the command parameter...
MainFrameWindow.WindowState = WindowState.Maximized;
}
public ApplicationMaximizeCommand()
{
}
}
This is what my xaml looks like:
<viewModel:MenuItemVM Name="AppMaxe" Content="SomeImage.png" Type="Image">
<viewModel:MenuItemVM.Command >
<commands:AppMaxCommand />
</viewModel:MenuItemVM.Command>
</viewModel:MenuItemVM>
Thanks,
JohnB
We have modified the sample in order to demonstrate you how to pass a specific parameter for the separate menu items - please, check it.
As already explained in my initial response a new additional property needs to be added inside the MenuItemViewModel and that property needs to be bound to the CommandParameter property of the MenuItem. Please, notice that if you need to set values through a Binding for the added inside the MenuItemViewModel property you need to make it a DependencyProperty.
Hope this helps.
Regards,
Nasko
Telerik by Progress