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

MouseDoubleClick fired for Master and Child

2 Answers 209 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ronny
Top achievements
Rank 1
Ronny asked on 24 Feb 2011, 06:51 PM
Hello,

when I am double clicking on a child row then also then also the event from parent grid will be fired, if a row is selected. How can I supress the event of the parent grid without deselecting the row?

In my example, I am using MVVM Light.

<Window x:Class="DoubleClickEventFiredTwice.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
        xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4" 
        Title="MainWindow" Height="350" Width="525" x:Name="window">
    <Grid>
        <telerik:RadGridView Margin="12" ShowGroupPanel="False" ItemsSource="{Binding Employees}"
                             AutoGenerateColumns="False" CanUserFreezeColumns="False" CanUserResizeColumns="False" IsReadOnly="True" 
                             SelectedItem="{Binding Path=SelectedEmployee, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" CanUserInsertRows="False" 
                             CanUserDeleteRows="False" DataLoadMode="Asynchronous" PreviewMouseDoubleClick="RadGridView_PreviewMouseDoubleClick">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn Header="Name" DataMemberBinding="{Binding Name, Mode=TwoWay}" Width="1*" />
            </telerik:RadGridView.Columns>
            <telerik:RadGridView.ChildTableDefinitions>
                <telerik:GridViewTableDefinition>
                    <telerik:GridViewTableDefinition.Relation>
                        <telerik:PropertyRelation ParentPropertyName="Notes" />
                    </telerik:GridViewTableDefinition.Relation>
                </telerik:GridViewTableDefinition>
            </telerik:RadGridView.ChildTableDefinitions>
            <telerik:RadGridView.HierarchyChildTemplate>
                <DataTemplate>
                    <telerik:RadGridView x:Name="GridViewDetail" CanUserFreezeColumns="False" AutoGenerateColumns="False" ItemsSource="{Binding Notes}" 
                                                 ShowGroupPanel="False" IsReadOnly="True" CanUserInsertRows="False" 
                                                 SelectedItem="{Binding Mode=TwoWay, Path=DataContext.SelectedNote, RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}}" 
                                                 CanUserDeleteRows="False" DataLoadMode="Asynchronous" PreviewMouseDoubleClick="RadGridView_PreviewMouseDoubleClick">
                        <telerik:RadGridView.Columns>
                            <telerik:GridViewDataColumn DataMemberBinding="{Binding Text, Mode=TwoWay}" Header="Note" Width="200" />
                        </telerik:RadGridView.Columns>
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="MouseDoubleClick">
                                <cmd:EventToCommand Command="{Binding Mode=OneWay, Path=DataContext.EditNoteCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}}"
                                                    PassEventArgsToCommand="True" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                    </telerik:RadGridView>
                </DataTemplate>
            </telerik:RadGridView.HierarchyChildTemplate>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="MouseDoubleClick">
                    <cmd:EventToCommand Command="{Binding EditEmployeeCommand, Mode=OneWay}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </telerik:RadGridView>
    </Grid>
</Window>

using System;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
 
namespace DoubleClickEventFiredTwice
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
 
            this.DataContext = new ViewModel();
        }
 
        private void RadGridView_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var clicked = e.OriginalSource as UIElement;
 
            e.Handled = clicked == null || clicked.ParentOfType<GridViewRow>() == null;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
 
namespace DoubleClickEventFiredTwice
{
    public class ViewModel
    {
        public ViewModel()
        {
            this.EditEmployeeCommand = new RelayCommand<RoutedEventArgs>(
                args => this.EditEmployee(this.SelectedEmployee),
                args => this.SelectedEmployee != null);
 
            this.EditNoteCommand = new RelayCommand<RoutedEventArgs>(
                args => this.EditNote(this.SelectedNote),
                args => this.SelectedNote != null);
        }
 
        public IList<Employee> Employees
        {
            get
            {
                return new List<Employee>
                       {
                           new Employee { Name = "Hans Meier", Notes = new[]{ new Note { Text = "Hinweis 1"}, new Note { Text = "Hinweis 2"}   }},
                           new Employee { Name = "Klaus Schuster", Notes = new[]{ new Note { Text = "Hinweis 3"}, new Note { Text = "Hinweis 4"}   }}
                       };
            }
        }
 
        public Employee SelectedEmployee { getset; }
 
        public Note SelectedNote { getset; }
 
        public ICommand EditEmployeeCommand { getprivate set; }
 
        public ICommand EditNoteCommand { getprivate set; }
 
        public void EditEmployee(Employee item)
        {
            MessageBox.Show(item.Name);
        }
 
        public void EditNote(Note item)
        {
            MessageBox.Show(item.Text);
        }
    }
 
    public class Employee
    {
        public String Name { getset; }
 
        public IList<Note> Notes { getset; }
    }
    public class Note
    {
        public String Text { getset; }
    }
}

Thanks and greetings,
Ronny

2 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 28 Feb 2011, 01:20 PM
Hi Ronny,

You may handle this scenario similarly as in this forum thread. However, instead of subscribing to SelectionChanged event, you may use the MouseDoubleClick event.

Regards,
Maya
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!
0
Ronny
Top achievements
Rank 1
answered on 03 Mar 2011, 10:47 AM
Hi Maya,

this not solved my problem. I had to change the code a litte:

private void GridView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    var model = this.DataContext as ViewModel;
    var depend = e.OriginalSource as DependencyObject;
 
    if (model == null || depend == nullreturn;
 
    // Thanks to: http://www.hardcodet.net/2008/02/find-wpf-parent
    var grid = UIHelper.TryFindParent<RadGridView>(depend);
 
    if (grid == null || sender != grid) return;
 
    if (grid == this.GridView)
    {
        model.EditMasterCommand.Execute(e);
    }
    else
    {
        model.EditDetailCommand.Execute(e);
    }
}
Greetings,
Ronny
Tags
GridView
Asked by
Ronny
Top achievements
Rank 1
Answers by
Maya
Telerik team
Ronny
Top achievements
Rank 1
Share this question
or