Untitled

To enable RadContextMenu for every grid data row you should do:

1) Handle RowLoaded event for the grid and create RadContextMenu:

void RadGridView1_RowLoaded(object sender, RowLoadedEventArgs e)
{
    if (!(e.Row is GridViewHeaderRow) && !(e.Row is GridViewNewRow))
    {
        RadContextMenu rowContextMenu = new RadContextMenu(); // create menu
        StyleManager.SetTheme(rowContextMenu, StyleManager.GetTheme(RadGridView1)); // set menu Theme

        // create menu items
        rowContextMenu.Items.Add(new RadMenuItem() { Header = "Show row ID property value" });
        rowContextMenu.Items.Add(new RadMenuItem() { Header = "Item2", IsEnabled = false });
        rowContextMenu.Items.Add(new RadMenuItem() { Header = "Item3", IsEnabled = false });
        
        // add menu events
        rowContextMenu.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
        rowContextMenu.Opened += new RoutedEventHandler(OnMenuOpened);
        
        // attach menu
        RadContextMenu.SetContextMenu(e.Row, rowContextMenu);
    }
}

2) Select the grid row on context menu open:

void OnMenuOpened(object sender, RoutedEventArgs e)
{
    GridViewRow row = ((RadRoutedEventArgs) e).OriginalSource as GridViewRow;
    if (row != null)
    {
        row.IsSelected = row.IsCurrent = true;
    }
}


3) Perform desired action when menu item is clicked:

void OnMenuItemClick(object sender, RoutedEventArgs e)
{
    RadMenuItem clickedItem = ((RadRoutedEventArgs) e).OriginalSource as RadMenuItem;
    if (clickedItem != null)
    {
        string header = Convert.ToString(clickedItem.Header);

        object selectedItem = RadGridView1.SelectedItem;

        switch (header)
        {
            case "Show row ID property value":
                int ID = ((MyBusinessObject)selectedItem).ID;
                DialogParameters parameters = new DialogParameters();
                parameters.Content = String.Format("ID: {0}", ID);
                parameters.Header ="Selected row ID property value:";
                parameters.Theme = StyleManager.GetTheme(RadGridView1);
                parameters.IconContent = null;
                RadWindow.Alert(parameters);
                break;
            default:
                break;
        }
    }
}


Please make sure also that you have <param name="windowless" value="true" /> for your SIlverlight object declaration!

Enjoy!

[Download]


About the Author

Vladimir Enchev

is Director of Engineering, Native Mobile UI & Frameworks

Comments

Comments are disabled in preview mode.