GridView DoubleClick MVVM

4 posts, 1 answers
  1. May
    May avatar
    14 posts
    Member since:
    Apr 2007

    Posted 25 May 2010 Link to this post

    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
  2. Answer
    Tsvyatko
    Admin
    Tsvyatko avatar
    863 posts

    Posted 27 May 2010 Link to this post

    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.
  3. May
    May avatar
    14 posts
    Member since:
    Apr 2007

    Posted 28 May 2010 Link to this post

    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
  4. Abolhassan
    Abolhassan avatar
    7 posts
    Member since:
    Aug 2012

    Posted 14 Oct 2014 Link to this post

    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.










Back to Top