Differentiate between left/right click

5 posts, 0 answers
  1. chris
    chris avatar
    22 posts
    Member since:
    Aug 2012

    Posted 20 Aug 2009 Link to this post

    I created a context menu for each row in a gridview following this example - http://blogs.telerik.com/vladimirenchev/posts/09-04-09/how_to_radgridview_for_silverlight_row_context_menu_in_three_simple_steps.aspx and it works fine without the SelectionChanged event.
    Now, the problem is that I want a user to be able to left-click a row and it will take them to another page:
    private void radGVInspectionForm_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e) 
            { 
                 
                ////Get the reference to the selected item 
                object mySelectedRow = radGVInspectionForm.SelectedItem; 
     
                ////Get the id of the form 
                string id = ((InspectionFormDS.InspectionFormTableRow)mySelectedRow).Id; 
                 
                ////Navigate to the edit form page 
                this.NavigationService.Navigate(new DisplayInspectionFormPage(Convert.ToInt32(id))); 
            } 
    But this code fires when I try to right-click a row to get to the context menu. So the SelectionChanged event fires for both left and right clicks.
    How can I figure out which mouse button was clicked in the SlectionChanged event so if a right button is clicked, then show context menu, if a left button is clicked, then navigate to other page?
  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 21 Aug 2009 Link to this post

    Hi chris,

    You should do the following. At the end of the OnRowLoaded method, attach to the row's MouseLeftButtonDown event and then in the handler do your thing, i.e. the one that you are now doing in OnSelectionChanged. For example:

    using System; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Input; 
    using Telerik.Windows; 
    using Telerik.Windows.Controls; 
    using Telerik.Windows.Controls.GridView; 
     
    namespace TicketID_236799_ContextMenuClick 
        public partial class MainPage : UserControl 
        { 
            public MainPage() 
            { 
                this.InitializeComponent(); 
     
                this.clubsGrid.RowLoaded += this.OnRowLoaded; 
     
                this.clubsGrid.ItemsSource = Club.GetClubs(); 
            } 
     
            private void OnRowLoaded(object sender, RowLoadedEventArgs e) 
            { 
                if (!(e.Row is GridViewHeaderRow) && !(e.Row is GridViewNewRow) && !(e.Row is GridViewFooterRow)) 
                { 
                    var rowContextMenu = new RadContextMenu(); // create menu 
                    StyleManager.SetTheme(rowContextMenu, StyleManager.GetTheme(this.clubsGrid)); // set menu Theme 
     
                    // create menu items 
                    rowContextMenu.Items.Add(new RadMenuItem() {Header = "Show row ID property value"}); 
                    rowContextMenu.Items.Add(new RadMenuItem() {Header = "Item2", IsEnabled = false}); 
                    rowContextMenu.Items.Add(new RadMenuItem() {Header = "Item3", IsEnabled = false}); 
     
                    // add menu events 
                    rowContextMenu.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(this.OnMenuItemClick)); 
                    rowContextMenu.Opened += new RoutedEventHandler(this.OnMenuOpened); 
     
                    // attach menu 
                    RadContextMenu.SetContextMenu(e.Row, rowContextMenu); 
     
     
                    // And now attach to the MouseClick event 
                    e.Row.MouseLeftButtonDown += new MouseButtonEventHandler(this.OnRowMouseLeftButtonDown); 
                } 
            } 
     
            private void OnRowMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
            { 
                var row = (GridViewRow) sender; 
     
                var club = (Club) row.DataItem; 
                MessageBox.Show("Left clicked on " + club.Name); 
            } 
     
            private void OnMenuOpened(object sender, RoutedEventArgs e) 
            { 
                var row = ((RadRoutedEventArgs) e).OriginalSource as GridViewRow; 
                if (row != null
                { 
                    row.IsSelected = row.IsCurrent = true
                } 
            } 
     
            private void OnMenuItemClick(object sender, RoutedEventArgs e) 
            { 
                var clickedItem = ((RadRoutedEventArgs) e).OriginalSource as RadMenuItem; 
                if (clickedItem != null
                { 
                    string header = Convert.ToString(clickedItem.Header); 
     
                    object selectedItem = this.clubsGrid.SelectedItem; 
     
                    switch (header) 
                    { 
                        case "Show row ID property value"
                            string name = ((Club) selectedItem).Name; 
                            var parameters = new DialogParameters(); 
                            parameters.Content = String.Format("Name: {0}", name); 
                            parameters.Header = "Selected row ID property value:"
                            parameters.Theme = StyleManager.GetTheme(this.clubsGrid); 
                            parameters.IconContent = null
                            RadWindow.Alert(parameters); 
                            break
                        default
                            break
                    } 
                } 
            } 
        } 

    I have attached a small sample project that demonstrates this.

    Kind regards,
    Ross
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WPF is Visual Studio 2017 Ready
  4. chris
    chris avatar
    22 posts
    Member since:
    Aug 2012

    Posted 21 Aug 2009 Link to this post

    Thank you, it works great!
  5. Steven
    Steven avatar
    7 posts
    Member since:
    Jul 2012

    Posted 15 Mar 2011 Link to this post

    Doesn't do a danged thing for me. The right-click fires but the left click doesn't :(

    Maybe because I'm using WPF and the download was Silverlight?
  6. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 16 Mar 2011 Link to this post

    Hello chris,

    Can you please explain in greater detail what are you trying to achieve? Maybe you can open a separate support ticket and attach a sample project that demonstrates what you are after.

    Greetings,
    Ross
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Back to Top
UI for WPF is Visual Studio 2017 Ready