Hi there,
I have a RadContextMenu on a TreeListView and I've successfully hooked up the context menu to a command in my MVVM view model like this...
...
<telerik:RadContextMenu.ItemContainerStyle>
<Style TargetType="telerik:RadMenuItem" BasedOn="{StaticResource RadMenuItemStyle}" d:DataContext="{d:DesignInstance data:IContextMenuItem}">
<Setter Property="Command" Value="{Binding Command}"/>
This gets fired on both left clicks on the menu item as well as right clicks on the menu item. Is it possible to handle right clicks separately?
Thanks
Russell
5 Answers, 1 is accepted
You can register a ClassHandler to handle the right click like so:
static MainWindow() { EventManager.RegisterClassHandler(typeof(RadMenuItem), Mouse.MouseDownEvent, new MouseButtonEventHandler(OnContextMenuOpening), false);} public static void OnContextMenuOpening(object sender, MouseButtonEventArgs e) { if (e.RightButton == MouseButtonState.Pressed) { // your logic here } }Please let me know if this works for you.
Regards,
Dilyan Traykov
Telerik
I just came across this post trying to do something similar and it isn't quite working for me. I'm trying to prevent the right click from executing the command, but it still is even though I am setting e.Handled = True. Am I missing something?
EventManager.RegisterClassHandler(GetType(Telerik.Windows.Controls.RadMenuItem), Mouse.MouseDownEvent, New MouseButtonEventHandler(AddressOf OnMenuItemMouseDown), False)Sub OnMenuItemMouseDown(sender As Object, e As MouseButtonEventArgs) If e.RightButton = MouseButtonState.Pressed Then e.Handled = True End IfEnd SubI've prepared a small sample project where the aforementioned approach works as expected.
Could you please have a look and let me know how this setup differs from the one you have at your end so that I can further assist you?
I look forward to your reply.
Regards,
Dilyan Traykov
Progress Telerik
Please excuse me for overlooking this.
You should handle the MouseRightButtonUp event in this scenario in the following manner:
Public Sub New() InitializeComponent() EventManager.RegisterClassHandler(GetType(RadMenuItem), RadMenuItem.MouseRightButtonUpEvent, New MouseButtonEventHandler(AddressOf RightButtonUp), False)End SubPublic Sub RightButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) e.Handled = TrueEnd SubPlease let me know if this works for you.
Regards,
Dilyan Traykov
Progress Telerik