Hello Henrik,
Thanks again for the great Feedback and sorry for not replying sooner.
- The RadContextMenu forum will be the RadMenu forum, it will be renamed soon.
- I am not sure why elementWithContextMenu is hidden, surely a public getter would not hurt. As far as I remember a ContextMenuService (like in WPF) is planned so one and the same menu can be used for multiple elements, so maybe this is why the property is internal. I will make it public if it's ok.
- Inheriting the DataContext is a great idea! Why didn't we think of it :).
I tried and implemented your specific scenario: The context menu is bound to the DataGridRow:
I used the EventManager and ClassHandlers. Handling a routed event on a class level means that you can add a method that will be invoked every time a specific event goes through an instance of a given class. This means that you can handle the ContextmenuOpen event on the GridRow and have both the ContextMenu (OriginalSource) and the GridRow there.
Handling Events at class level are helpful because you don't have to add a handler for each instance and you do not need direct access to the instances.
One more thing: Whenever you see RoutedEventArgs, cast them to RadRoutedEventArgs or their subclass. Some of the RoutedEventArgs's properties were made internal and we cannot set them anymore, this is not a problem with our event args though.
Here is the code for the example:
//Static Constructor: |
static ContextMenuInDataGrid() |
{ |
EventManager.RegisterClassHandler(typeof(DataGridRow), RadContextMenu.OpenedEvent, new RoutedEventHandler(OnContextMenuOpen)); |
} |
|
private static void OnContextMenuOpen(object sender, RoutedEventArgs e) |
{ |
var radE = e as RadRoutedEventArgs; |
var contextMenu = radE.OriginalSource as RadContextMenu; |
var gridRow = sender as DataGridRow; |
|
contextMenu.DataContext = gridRow.DataContext; |
} |
|
And the xaml:
<data:DataGrid x:Name="dataGrid" AutoGenerateColumns="False"> |
<data:DataGrid.Columns> |
<data:DataGridTemplateColumn Header="E"> |
<data:DataGridTemplateColumn.CellTemplate> |
<DataTemplate> |
<Button Content="E"> |
<nav:RadContextMenu.ContextMenu> |
<nav:RadContextMenu EventName="Click" |
ItemsSource="{Binding MenuItems}" /> |
</nav:RadContextMenu.ContextMenu> |
</Button> |
</DataTemplate> |
</data:DataGridTemplateColumn.CellTemplate> |
</data:DataGridTemplateColumn> |
<data:DataGridTextColumn Binding="{Binding Name}" Header="Name"/> |
<data:DataGridCheckBoxColumn Binding="{Binding IsSomething}" Header="Is Something"/> |
<data:DataGridTextColumn Binding="{Binding Value}" |
Header="Value" /> |
</data:DataGrid.Columns> |
</data:DataGrid> |
All the best,
Miroslav
the Telerik team
Check out
Telerik Trainer, the state of the art learning tool for Telerik products.