ContextMenu MouseRightButtonUp Bug?

4 posts, 0 answers
  1. Benny
    Benny avatar
    33 posts
    Member since:
    May 2009

    Posted 11 Feb 2011 Link to this post


    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>
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 11 Feb 2011 Link to this post

    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>>
  3. DevCraft banner
  4. Benny
    Benny avatar
    33 posts
    Member since:
    May 2009

    Posted 14 Feb 2011 Link to this post

    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);

  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 14 Feb 2011 Link to this post

    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>>
Back to Top