DataGrid row double click as command

Mar 25, 2009 at 6: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.

regards,
j23tom
Coordinator
Mar 25, 2009 at 7:01 PM
You can used an attached behavior.

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

public

class ClickBehavior
{
    #region DoubleClick

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

    public static void SetOnDoubleClick(DependencyObject target, ICommand value)
    {
        target.SetValue(
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);
        command.Execute(
null);
    }

    #endregion DoubleClick
}


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

 



Mar 26, 2009 at 1: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?
Coordinator
Mar 26, 2009 at 8:23 PM
SetOnDoubleClick() is being used.  See this overview for info on how to use attached properties, http://msdn.microsoft.com/en-us/library/ms749011.aspx

You'll have to show a little bit more infor than that.  What does your code look like?
Mar 28, 2009 at 9:03 AM
Edited Mar 28, 2009 at 9: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}"
                           SelectionMode="Single"
                           SelectedItem="{Binding Mode=TwoWay,Path=SelectedItem}"
                           AutoGenerateColumns="False">
               <toolkit:DataGrid.RowStyle>
                    <Style>
                         <Setter Property="controls:ClickBehavior.OnDoubleClick" Value="{Binding MyCommand}"/>
                    </Style>
               </toolkit:DataGrid.RowStyle>
               <toolkit:DataGrid.Columns>               
                    <toolkit:DataGridTextColumn CanUserReorder="False"    IsReadOnly="True" Header="Name"   Binding="{Binding Name}"  />  
               </toolkit:DataGrid.Columns>
</toolkit:DataGrid >

PS: I found another intersting solution at:  http://www.japf.fr/?p=22




Mar 22, 2010 at 4:28 PM

Thank you.  Worked like a charm.