Telerik UI for Windows Phone by Progress

RadContextMenu supports only RadContextMenuItem objects when the menu is populated manually (unbound mode). RadContextMenuItem is a ContentControl that exposes a few unique properties. These are Command and CommandParameter.

The Command property

The command property is of type ICommand. When Command is set to a non-null value tapping on an item executes the command if the CanExecute method returns true for the current command parameter. If Command is not set, the content of the menu item will be cast as ICommand. This allows RadContextMenu to be bound to a list of commands and it will just work.

Tip
The Tapped event fires before the command's Execute method is called.

The CommandParameter property

The CommandParameter property is used internally by RadContextMenuItem to determine if the current command can execute. If CommandParameter is not set the focused element of the owner menu will be passed to CanExecute of the current command. If the focused element is also null the original source of the open manipulation of RadContextMenu is passed to the CanExecute method of the command.

Note
The Command pattern implementation of the application that uses RadContextMenu should be aware that the CanExecute method of a command can be called an undefined number of times before the Execute method is called.

Using commands

Below is a simple implementation of the ICommand interface and an example of how to use this implementation with RadContextMenu.

CopyXAML
<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <local:ChangeColorCommand x:Key="ChangeColorCommand"/>
        <SolidColorBrush Color="Green" x:Key="GreenBrush"/>
    </Grid.Resources>

    <Rectangle Width="200"
               Height="200"
               Fill="{StaticResource PhoneForegroundBrush}"
               x:Name="rectangle">
        <telerikPrimitives:RadContextMenu.ContextMenu>
            <telerikPrimitives:RadContextMenu OpenGesture="Tap"
                                              IsZoomEnabled="False">
                <telerikPrimitives:RadContextMenuItem Content="change color"
                                                      Command="{StaticResource ChangeColorCommand}"
                                                      CommandParameter="{StaticResource GreenBrush}"/>
                <telerikPrimitives:RadContextMenuItem Content="reset color"
                                                      Command="{StaticResource ChangeColorCommand}"
                                                      CommandParameter="{StaticResource PhoneForegroundBrush}"/>
            </telerikPrimitives:RadContextMenu>
        </telerikPrimitives:RadContextMenu.ContextMenu>
    </Rectangle>
</Grid>
CopyC#
public partial class MainPage : PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
        ChangeColorCommand command = (ChangeColorCommand)this.LayoutRoot.Resources["ChangeColorCommand"];
        command.Rectangle = this.rectangle;
    }
}

public class ChangeColorCommand : ICommand
{
    public Rectangle Rectangle
    {
        get;
        set;
    }

    public bool CanExecute(object parameter)
    {
        return parameter is Brush;
    }

    public event System.EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        if (this.Rectangle == null)
        {
            return;
        }

        this.Rectangle.Fill = parameter as Brush;
    }
}