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

GridView DoubleClick MVVM

5 Answers 1223 Views
GridView
This is a migrated thread and some comments may be shown as answers.
May
Top achievements
Rank 1
May asked on 26 May 2010, 12:30 AM
Hi there,

In this link http://www.telerik.com/community/code-library/wpf/general/mvvm-context-amp-row-doubleclick-functionality.aspx, it is using the CommandBindingBehavior to bind the MouseDoubleClick event. The Problem is the the even fires when dbl clicking the header or scrollbar.

How can I make it fire when the actual row or cell is double clicked?

Thanks!

May

5 Answers, 1 is accepted

Sort by
0
Accepted
Tsvyatko
Telerik team
answered on 27 May 2010, 11:51 AM
Hello May,

In order to fire the click event only when click is performed on a row the following code is needed:
private void RGV_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    e.Handled = ((FrameworkElement)e.OriginalSource).ParentOfType<GridViewRow>() == null;
}

I have attached sample project that demonstrates its usage. Please, let me know if this works for you.

Best wishes,
Tsvyatko
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
May
Top achievements
Rank 1
answered on 28 May 2010, 06:22 PM
Hi Tsvyatko,

Thanks for the answer!

In the future, can a property added to cancel to handle the dbl click event if the OriginalSource is not GridViewRow. Since we use the GridView a lot, we have to wrap the GridView control to handle it. It would be nice have a property to set it.

Thanks again!

May
0
Abolhassan
Top achievements
Rank 1
answered on 14 Oct 2014, 05:02 AM
Here's my solution for this:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Telerik.Windows.Controls;
 
namespace MyApp.Utility
{
    public sealed class RadGridViewRowDoubleClickHandler : FrameworkElement
    {
        public RadGridViewRowDoubleClickHandler(RadGridView gridView)
        {
            MouseButtonEventHandler handler = (sender, args) =>
            {
                var methodName = GetMethodName(gridView);
 
                var dataContextType = gridView.DataContext.GetType();
                var method = dataContextType.GetMethod(methodName);
                if (method == null)
                {
                    throw new MissingMethodException(methodName);
                }
 
                method.Invoke(gridView.DataContext, null);
            };
 
            gridView.RowLoaded += (s, e) =>
            {
                e.Row.MouseDoubleClick += handler;
            };
 
            gridView.RowUnloaded += (s, e) =>
            {
                e.Row.MouseDoubleClick -= handler;
            };
        }
 
        public static string GetMethodName(RadGridView gridView)
        {
            return (string)gridView.GetValue(MethodNameProperty);
        }
 
        public static void SetMethodName(RadGridView gridView, string value)
        {
            gridView.SetValue(MethodNameProperty, value);
        }
 
        public static readonly DependencyProperty MethodNameProperty = DependencyProperty.RegisterAttached(
            "MethodName",
            typeof(string),
            typeof(RadGridViewRowDoubleClickHandler),
            new PropertyMetadata((o, e) =>
            {
                var gridView = o as RadGridView;
                if (gridView != null)
                {
                    new RadGridViewRowDoubleClickHandler(gridView);
                }
            }));
    }
}

xmlns:local.Utility="clr-namespace:MyApp.Utility"
<telerik:RadGridView Grid.Row="1"
                             ItemsSource="{Binding Reports.ReportCollection}"
                             SelectedItem="{Binding SelectedReport, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                             local.Utility:RadGridViewRowDoubleClickHandler.MethodName="ShowReportDetails"> ...

"ShowReportDetails" is a public method in ViewModel which I get the SelectedItem(the row that was clicked) and show a DialogBox and display my report.

Hope it helps.










0
Chris
Top achievements
Rank 1
Iron
answered on 25 Apr 2021, 03:32 PM
You a correct, you shouldn't cast to FrameworkElement.  Cast to DependencyObject.  If you double clicked something like a Run, that will fail the cast to FrameworkElement.
0
Chris
Top achievements
Rank 1
Iron
answered on 25 Apr 2021, 03:33 PM

Ok I mistyped that somehow.  Here it is corrected:

Just a correction, you shouldn't cast to FrameworkElement.  Cast to DependencyObject.  If you double clicked something like a Run, that will fail the cast to FrameworkElement.

Tags
GridView
Asked by
May
Top achievements
Rank 1
Answers by
Tsvyatko
Telerik team
May
Top achievements
Rank 1
Abolhassan
Top achievements
Rank 1
Chris
Top achievements
Rank 1
Iron
Share this question
or