Getting row information after a doubleclick

Josh picture Josh · Feb 28, 2012 · Viewed 7.6k times · Source

I am trying to retrieve row info from a datagrid after a double click event. I have the event setup, but now I just need to setup the function to retrieve the data from the row.

XAML:

    <DataGrid 
        Width="Auto" 
        SelectionMode="Extended" 
        IsReadOnly="True" 
        Name="ListDataGrid"
        AutoGenerateColumns="False"
        ItemsSource="{Binding ListFieldObject.MoviesList}"
        DataContext="{StaticResource MovieAppViewModel}"
        cal:Message.Attach="[Event MouseDoubleClick] = [Action RowSelect()]">

        <DataGrid.Columns>
            <DataGridTextColumn Width="200" IsReadOnly="True" Header="Title" Binding="{Binding Title}"/>
            <DataGridTextColumn Width="100" IsReadOnly="True" Header="Rating" Binding="{Binding Rating}"/>
            <DataGridTextColumn Width="100" IsReadOnly="True" Header="Stars" Binding="{Binding Stars}"/>
            <DataGridTextColumn Width="93" IsReadOnly="True" Header="Release Year" Binding="{Binding ReleaseYear}"/>
        </DataGrid.Columns>
    </DataGrid>

C# (MVVM ViewModel):

     public void RowSelect()
     {
         //now how to access the selected row after the double click event?
     }

Thanks Much!

Answer

David Kiff picture David Kiff · Jun 25, 2013

You can alternatively do this:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="cal:Message.Attach" Value="[MouseDoubleClick] = [Action RowSelect($dataContext)]"/>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

Then

public void RowSelect(MoviesListItem movie)
{
     //now how to access the selected row after the double click event?
}