Context menu in gridview

5 posts, 1 answers
  1. Paul
    Paul avatar
    26 posts
    Member since:
    Jan 2012

    Posted 17 May 2012 Link to this post

    Hi
    No forum for Context menu control so will post it here.

    I'm trying to do something that would seem silmple but am having problems with bindings.
    Scenario:

    I have a radtabitem that has its datacontext set to a viewmodel, call it 'MainViewModel'

    I have a radgridview inside the radtabitem that is bound to property on 'MainViewModel' which is an observablecollection<SubViewModel>
     
    When I rightclick a radgridview row I would like a context menu with 2 menu items 'Save' and 'Cancel'.

    The 'Save' and 'Cancel' menu items should be bound to commands in 'SubViewModel', also 'SubViewMenu' exposes a 'IsDirty' property which should be used to set the enabled property of the menu items.

    When checking the output window, 'Save', 'Cancel' and 'IsDirty' can't be found on 'MainViewModel'.

    I have set the property on the context menu InheritDataContext="True" thinking that it would use the DataContext of the radgridview which is where the properties are that I'm trying to bind to.  This doesn't work for me.

    Silverlight 5
    Current build of Telerik Silverlight controls
    MVVM

    Can anybody help me with this?

    Thanks
    Paul
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 17 May 2012 Link to this post

    Hello,

    You can check this help article where adding a context menu is explained in great details. A sample project can be downloaded here.

    Kind regards,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  3. Paul
    Paul avatar
    26 posts
    Member since:
    Jan 2012

    Posted 17 May 2012 Link to this post

    Hi Didie

    I did come across that article.  Seems like a lot of code just to get a context menu to work.  Why do I have to create a class decorated with an attribute([ContentProperty("ContextMenu"])?  Can you point me to an article explaining why this is so?

    Anyway the article is for a context menu for the columns in the grid, I just want to have a context menu for the entire row of the grid.When I change the binding 'Binding Path=UIElement.Column.DataControl.DataContext' to 'Binding Path=UIElement.Row.DataControl.DataContext' I get error complaining about 'Row'.

    Thanks for your reply Didie.
    Paul

  4. Answer
    Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 17 May 2012 Link to this post

    Hi,

     Indeed, this example is not about context menu on rows.
    As I understood your GridView is bound to ObservableCollection<SubViewModel> and its DataContext is "MainViewModel".  You should specify the Source of the Binding to be the DataContext of the Row, not the DataContext of the GridView. You can set the right DataContext once the ContextMenu is opened. Can you use an approach similar to the one suggested in this online demo?

    Greetings,
    Didie
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. Paul
    Paul avatar
    26 posts
    Member since:
    Jan 2012

    Posted 17 May 2012 Link to this post

    Hi Didie

    I was able to get it to work by modifying the behavior to set the datacontext of the contextmenu to the datacontext of the row that was selected as follows:

    private void RadContextMenu_Opened(object sender, RoutedEventArgs e)

    {

    RadContextMenu menu = (RadContextMenu)sender;

    GridViewRow row = menu.GetClickedElement<GridViewRow>();

    if (row != null)

    {

    row.IsSelected = row.IsCurrent = true;

    menu.DataContext = row.DataContext;

    }

    else

    {

    menu.IsOpen = false;

    }

    }

    Thanks for pointing me in the right direction
    Paul

Back to Top