ContextMenu and MVVM

So far you have seen the ContextMenu employed with hardwired MenuItems and Click events. This approach is adequate for simple apps. Yet, the MenuItem class supports ICommands out of the box. And, with this, you can bind a viewmodel’s ICommand to a MenuItem, removing the need for Click event handlers in the code-beside.

To demonstrate, we bind a viewmodel command to a MenuItem, which displays a message to the user. The ContextMenuViewModel class, in the downloadable sample code, contains a ShowMessageCommand property, declared as shown:

readonly DelegateCommand showMessageCommand;public ICommand ShowMessageCommand{    get    {        return showMessageCommand;    }}

The task of the ShowMessageCommand ...

Get Windows® Phone 8 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.