Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse Double Click DataGrid row

I have a DataGrid style template that I wish to add double click behaviour to. The binding should be correct but I cannot seem to get the xaml compiling / working.

All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.

What is wrong with the code below?

<Style TargetType="{x:Type DataGridRow}">
    <EventSetter Event="MouseDoubleClick" Handler="{Binding Connect}"/>

Update per Viktor's comment (gives exact same error):

<Style x:Key="dataGridRowStyle" TargetType="{x:Type DataGridRow}">
    <EventSetter Event="PreviewMouseDoubleClick" Handler="{Binding Connect}"/>
like image 282
Chris Avatar asked Dec 08 '25 10:12

Chris


2 Answers

One can use DataGrid InputBindings to achieve goal:

<DataGrid.InputBindings>
   <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding SomeCommand}" />
</DataGrid.InputBindings>
like image 193
Igor Avatar answered Dec 10 '25 00:12

Igor


You can apply the following behavior on data grid row and follow the usage for implementation.

Double Click Behavior

    public class DoubleClickBehavior
    {
        #region DoubleClick

        public static DependencyProperty OnDoubleClickProperty = DependencyProperty.RegisterAttached(
            "OnDoubleClick",
            typeof(ICommand),
            typeof(DoubleClickBehavior),
            new UIPropertyMetadata(DoubleClickBehavior.OnDoubleClick));

        public static void SetOnDoubleClick(DependencyObject target, ICommand value)
        {
            target.SetValue(OnDoubleClickProperty, value);
        }

        private static void OnDoubleClick(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            var element = target as Control;
            if (element == null)
            {
                throw new InvalidOperationException("This behavior can be attached to a Control item only.");
            }

            if ((e.NewValue != null) && (e.OldValue == null))
            {
                element.MouseDoubleClick += MouseDoubleClick;
            }
            else if ((e.NewValue == null) && (e.OldValue != null))
            {
                element.MouseDoubleClick -= MouseDoubleClick;
            }
        }

        private static void MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            UIElement element = (UIElement)sender;
            ICommand command = (ICommand)element.GetValue(OnDoubleClickProperty);
            command.Execute(null);
        }

        #endregion DoubleClick
    }

Usage

        <Style   BasedOn="{StaticResource {x:Type DataGridRow}}"
               TargetType="{x:Type DataGridRow}">
            <Setter Property="Helpers:DoubleClickBehavior.OnDoubleClick" Value="{Binding Path=DataContext.MyCommandInVM, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ViewLayer:MyUserControl}}}" />
        </Style>
like image 44
Akanksha Gaur Avatar answered Dec 10 '25 00:12

Akanksha Gaur