DataGrid row double click as command

Mar 25, 2009 at 7:40 AM
Hi there,

What would be the best approach to make my datagrid's row double click event invoke command (which is bound from my ViewModel ) ?
The simplest one is to handle event in code-behind file and invoke command explicit from there, but I would like to have 'clean' code behind file
(if it is possible). Any suggestions would be appreciated.

Mar 25, 2009 at 8:01 PM
You can used an attached behavior.

Here is an example (on a TreeViewItem but can be adapted to a DataGridRow):


class ClickBehavior
    #region DoubleClick

    public static DependencyProperty OnDoubleClickProperty = DependencyProperty.RegisterAttached(
        new UIPropertyMetadata(ClickBehavior.OnDoubleClick));

    public static void SetOnDoubleClick(DependencyObject target, ICommand value)
ClickBehavior.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(ClickBehavior.OnDoubleClickProperty);

    #endregion DoubleClick

TargetType='{x:Type TreeViewItem}'>
    <Setter Property="local:ClickBehavior.OnDoubleClick" Value="{Binding OnDoubleClickCommand}" />


Mar 26, 2009 at 2:45 PM
This don't work on me!
You never use SetOnDoubleClick() method why do you have it?
And i have this Error:
"Error 1 Value 'local:ClickBehavior.OnDoubleClick' cannot be assigned to property 'Property'. Invalid PropertyDescriptor value."
So...what now?
Mar 26, 2009 at 9:23 PM
SetOnDoubleClick() is being used.  See this overview for info on how to use attached properties,

You'll have to show a little bit more infor than that.  What does your code look like?
Mar 28, 2009 at 10:03 AM
Edited Mar 28, 2009 at 10:28 AM
This works ! Thanks vinsibal! :) (VS2008 view crashes but it work)
Below working example for datagrid (every item in bound MyItems collection must have 'MyCommand'  ICommand property).

<toolkit:DataGrid ItemsSource="{Binding MyItems}"
                           SelectedItem="{Binding Mode=TwoWay,Path=SelectedItem}"
                         <Setter Property="controls:ClickBehavior.OnDoubleClick" Value="{Binding MyCommand}"/>
                    <toolkit:DataGridTextColumn CanUserReorder="False"    IsReadOnly="True" Header="Name"   Binding="{Binding Name}"  />  
</toolkit:DataGrid >

PS: I found another intersting solution at:

Mar 22, 2010 at 5:28 PM

Thank you.  Worked like a charm.