This is a migrated thread and some comments may be shown as answers.

Context menu in gridview

4 Answers 104 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paul
Top achievements
Rank 1
Paul asked on 17 May 2012, 01:00 PM
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

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 17 May 2012, 01:33 PM
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 >>

0
Paul
Top achievements
Rank 1
answered on 17 May 2012, 01:47 PM
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

0
Accepted
Dimitrina
Telerik team
answered on 17 May 2012, 02:29 PM
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 >>

0
Paul
Top achievements
Rank 1
answered on 17 May 2012, 06:33 PM
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

Tags
General Discussions
Asked by
Paul
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Paul
Top achievements
Rank 1
Share this question
or