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

Differentiate between left/right click

4 Answers 304 Views
GridView
This is a migrated thread and some comments may be shown as answers.
chris
Top achievements
Rank 1
chris asked on 21 Aug 2009, 03:30 AM
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?

4 Answers, 1 is accepted

Sort by
0
Rossen Hristov
Telerik team
answered on 21 Aug 2009, 02:29 PM
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.
0
chris
Top achievements
Rank 1
answered on 21 Aug 2009, 06:49 PM
Thank you, it works great!
0
Steven
Top achievements
Rank 1
answered on 16 Mar 2011, 12:15 AM
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?
0
Rossen Hristov
Telerik team
answered on 16 Mar 2011, 10:05 AM
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!
Tags
GridView
Asked by
chris
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
chris
Top achievements
Rank 1
Steven
Top achievements
Rank 1
Share this question
or