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

Trying to detect single click

6 Answers 97 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Daniel Billingsley
Top achievements
Rank 1
Daniel Billingsley asked on 26 Jul 2010, 08:46 PM
I have a GridView with a GridViewSelectColumn along with some others.  I want to take some action when a user clicks on a row, but not when the row is selected with the GridViewSelectColumn.  I have the SelectionMode set to Extended, and that gets me close, since clicking on a row selects just that row, but multiple rows can be selected with the checkbox.

However, the SelectionChanged event doesn't distinguish between selecting a row by clicking on it and selecting the row with the checkbox.  Is there a way to detect a single click on a row but also distinguish it from the selection via the checkbox?

Thanks.

6 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 28 Jul 2010, 01:16 PM
Hello Daniel Billingsley,

You can use our RowLoaded event to subscribe for MouseLeftButtonDown event:

public MainPage()
{
    InitializeComponent();
  
    this.playersGrid.RowLoaded += new EventHandler<RowLoadedEventArgs>(playersGrid_RowLoaded);
}
  
void playersGrid_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
{
    var row = e.Row as GridViewRow;
  
    if (row != null)
    {
        this.AddHandler(GridViewRow.MouseLeftButtonDownEvent, 
            new MouseButtonEventHandler(OnMouseLeftDown), true);
    }
}
  
public void OnMouseLeftDown(object sender, MouseButtonEventArgs e)
{
  
}


Sincerely yours,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Daniel Billingsley
Top achievements
Rank 1
answered on 28 Jul 2010, 03:42 PM
Could you explain how that's supposed to work? 

You're adding the handler to the mouse down event of the whole page.

Even when I replace your "this.AddHandler..." with "row.AddHandler..." I still get the event when the user clicks on the checkbox.

As I said, I want to detect any click on the row, but I ALSO need to determine if it was from selecting the row with the checkbox.
0
Milan
Telerik team
answered on 28 Jul 2010, 05:14 PM
Hello Daniel Billingsley,

You can update the OnMouseLeftDown to determine if CheckBox of the select column was clicked:

public void OnMouseLeftDown(object sender, MouseButtonEventArgs e)
{
    var clickedElement = e.OriginalSource as FrameworkElement;
    var parentCell = clickedElement.ParentOfType<GridViewCell>();
  
    if (parentCell != null && parentCell.Column is GridViewSelectColumn)
    {
        var parentCheckBox = clickedElement.ParentOfType<CheckBox>();
  
        if (parentCheckBox != null)
        {
            // checkbox was clicked
        }
    }
}


Best wishes,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Daniel Billingsley
Top achievements
Rank 1
answered on 28 Jul 2010, 07:13 PM
OK, but if I'm just going to be doing that, why can't I just hook to the page's mouse event right after InitializeComponent()?  Why run that code repeatedly for every row added to the grid when I'm just hooking to the Page's event anyway?
0
Milan
Telerik team
answered on 29 Jul 2010, 04:49 PM
Hi Daniel Billingsley,

There should be not problem to hook up to the Page's event instead.


Greetings,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Daniel Billingsley
Top achievements
Rank 1
answered on 29 Jul 2010, 08:31 PM
It seems that those events don't flow down through the visual tree.  For example, when hooked to the page's events i don't get anything when the click is inside the grid.  The way you did it might be the only way, but I'm not sure it's worth it to get what I want.

Thanks for the help.
Tags
GridView
Asked by
Daniel Billingsley
Top achievements
Rank 1
Answers by
Milan
Telerik team
Daniel Billingsley
Top achievements
Rank 1
Share this question
or