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}"/>

Answers


One can use DataGrid InputBindings to achieve goal:

<DataGrid.InputBindings>
   <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding SomeCommand}" />
</DataGrid.InputBindings>

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>

Not sure if you're going the MVVM route, but I've achieved this functionality using an Attached Command Behavior to wire up the double click event to a command in my viewmodel (where "command" is a reference to my attachedCommandBehavior assembly/class):

<DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="command:CommandBehavior.Event" Value="MouseDoubleClick"/>
            <Setter Property="command:CommandBehavior.Command" Value="{Binding SelectItemCmd}"/>
            <Setter Property="command:CommandBehavior.CommandParameter" Value="{Binding }"/>
        </Style>
</DataGrid.RowStyle>

Need Your Help

C# - synchronizing two threads with AutoResetEvent

c# autoresetevent

I'm trying to implement AutoResetEvent. For the purpose I use a very simple class :

iOS UIScrollView pinch zoom adds white space

ios objective-c uiscrollview uiimageview pinchzoom

I'm implementing pinch to zoom of an image using UIImageView inside a UIScrollView. The scroll view doesn't take full screen size. I made the background of the scrollview green. I then pinch-zoomed...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.