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

Need Advice on GridView and ContextMenu

2 Answers 70 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rayne
Top achievements
Rank 1
Rayne asked on 26 Jul 2011, 01:44 PM
I have a gridView that is part of a DataTemplate for a TabControl. One of my columns has a cell edit template that contains a textbox with a context menu. I need to bind the context menu to a collection on my viewmodel. but because it's not part of the virtual tree, I couldn't define the source in xaml. I managed to get it working by handling the Opened event and setting the DataContext of the contextmenu to the same as the user control. It all worked well, but then I ran into some trouble.

Elsewhere in code, I need to be able to call GridView.CommitEdit(), so I moved the DataTemplate code for the TabControl to a new user control to be able to call this method. Doing this however broke my code that sets the DataContext of the Context menu, because the DC of the new user control is not the view model...it is the entityObject to which its bound since it's a DataTemplate.

Is there anyway I can call the CommitEdit method from code-behind if the grid view is defined in a DataTemplate? Or possibly define the context menu elsewhere so that the datacontext can be set on it?

2 Answers, 1 is accepted

Sort by
0
Rayne
Top achievements
Rank 1
answered on 26 Jul 2011, 01:55 PM
I guess posting here opens my brain. I got it working by first calling GetVisualParent<T>() on the userControl to get the appropriate UserControl. Then I was able to set the DataContext of the context menu, to the datacontext of this parent control.

I'd still like to know if there is anyway to call methods of controls that are defined in a DataTemplate without moving those controls to their own userControl?
0
Rossen Hristov
Telerik team
answered on 26 Jul 2011, 02:18 PM
Hello Rayne,

The best approach would be the encapsulation into a new user control. If this user control has commands and dependency properties, you can create the appropriate Bindings and when you set its DataContext to the appropriate view model you will fully take advantage of WPF capabilities.

Alternatively, you could search down and up the visual tree with methods like ChildrenOfType<T> and ParentOfType<T> which are Telerik extension methods and are located in the Telerik.Windows.Controls namespace.

I hope this helps.

Best wishes,

Ross
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
GridView
Asked by
Rayne
Top achievements
Rank 1
Answers by
Rayne
Top achievements
Rank 1
Rossen Hristov
Telerik team
Share this question
or