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

ContextMenu MouseRightButtonUp Bug?

3 Answers 71 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Benny
Top achievements
Rank 1
Benny asked on 11 Feb 2011, 12:45 PM

MouseRightButtonUp only works then binded from xaml. When binding from code behinde it will not fire.

public partial class MainPage : UserControl
 {
     public MainPage()
     {
         InitializeComponent();

         image1.MouseRightButtonDown += new MouseButtonEventHandler(image_MouseRightButtonDown);
         image1.MouseRightButtonUp += new MouseButtonEventHandler(image_MouseRightButtonUp);
 
         image3.MouseRightButtonDown += new MouseButtonEventHandler(image_MouseRightButtonDown);
         image3.MouseRightButtonUp += new MouseButtonEventHandler(image_MouseRightButtonUp);
     }
 
     void image_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
     {
         MessageBox.Show("Works");
     }
 
     void image_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
     {
         e.Handled = true;
     }
 }

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="SLContextRightClick.MainPage"
                  xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
  <Grid x:Name="LayoutRoot">
        <Grid.RowDefinitions>
            <RowDefinition Height="150" />
            <RowDefinition Height="172" />
            <RowDefinition Height="158*" />
        </Grid.RowDefinitions>
 
        <!-- Event binded in code behinde will not work with  RadContextMenu.ContextMenu :-( -->
        <Image Height="150" HorizontalAlignment="Left" Margin="187,0,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="200" Source="/SLContextRightClick;component/Images/Chrysanthemum.jpg" >
            <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu x:Name="contextMenu" >
                    <telerik:RadMenuItem Header="Set Vista as Background" />
                    <telerik:RadMenuItem Header="Set Beach as Background" />
                    <telerik:RadMenuItem Header="Set Forest as Background" />
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>
        </Image>
        <Image Grid.Row="1" Height="150" HorizontalAlignment="Left" Margin="306,0,0,0" Name="image2" Stretch="Fill"
               VerticalAlignment="Top" Width="200" Source="/SLContextRightClick;component/Images/Penguins.jpg"
               MouseRightButtonDown="image_MouseRightButtonDown" MouseRightButtonUp="image_MouseRightButtonUp" >
            <telerik:RadContextMenu.ContextMenu>
                <telerik:RadContextMenu x:Name="contextMenu2" >
                    <telerik:RadMenuItem Header="Set Vista as Background" />
                    <telerik:RadMenuItem Header="Set Beach as Background" />
                    <telerik:RadMenuItem Header="Set Forest as Background" />
                </telerik:RadContextMenu>
            </telerik:RadContextMenu.ContextMenu>
        </Image>
 
        <Image Height="150" HorizontalAlignment="Left" Margin="164,10,0,0" Name="image3" Stretch="Fill"
               VerticalAlignment="Top" Width="200" Source="/SLContextRightClick;component/Images/Tulips.jpg"
              Grid.Row="2">
          
        </Image>
    </Grid>
</UserControl>

3 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 11 Feb 2011, 03:50 PM
Hi Benny,

When you attach event handler from XAML and then attach RadContextMenu context menu will add handler to MouseRightButtonDown/Up after your handler. But when you do if from code then RadContextMenu handler will be first and then yout handler will be second. And if you handle the event (e.Handled=true) which RadContextMenu do the second handler will not be invoked.

So i suggest that you add RadContextMenu and then from code add mouse handlers using the AddHanler method with third parameter TRUE (see UIElement.AddHandler method).

When last parameter is true then your handler will get invoked even if radContextMenu handles the event.

Let us know if you need more information.

Regards,
Hristo
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Benny
Top achievements
Rank 1
answered on 14 Feb 2011, 11:17 AM
Hi,
the problem is that there is no event UIElement.MouseRightButtonUpEvent.

Left button no problem....
image1.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(image_MouseRightButtonUp),   true);

But UIElement doesn't have a  UIElement.MouseRightButtonUpEvent so this will not compile :-(
image1.AddHandler(UIElement.MouseRightButtonUpEvent new MouseButtonEventHandler(image_MouseRightButtonUp),   true);

0
Hristo
Telerik team
answered on 14 Feb 2011, 11:48 AM
Hi Benny,

I'm sorry for misleading you. This event is available only in WPF.
The only solution that I can see (because there is no RoutedEvent for MouseRight...Event) is to attach the context menu on MouseRightButtonDown event (e.g. set EventName="MouseRightButtonDown" on the contextMenu).
This way you will be able to add handler to MouseRightButtonUp event (through code or XAML) and this handler will be called.

I hope that this is acceptable for you.

Kind regards,
Hristo
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
Menu
Asked by
Benny
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Benny
Top achievements
Rank 1
Share this question
or