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!
You can alternatively do this:
Then
(hope it will help) I am not sure about your case, but this is what I do in winforms:
You can do this by modifying the control template for the DataGridRows exposed by the DataGrid. The example below uses WPF and the Aero theme.
The only thing I've done is removed your previous cal:Message.Attach call and move it to a new "placeholder" ContentControl that surrounds the Border (x:Name=DGR_Border) in the "default" control template. (I used ContentControl because it has no visuals of its own and it exposes a MouseDoubleClick event.)
The only other thing you'll have to do is modify your RowSelect() method to accept a parameter of whatever type you're using here (I just assumed it was a 'Movie' type).
With Caliburn it is very easy, just pass $dataContext on your XAML:
And change your method to:
//EDIT Sorry, the above solution will work only if the action is on the datatemplate itself... another solution would be to have a SelectedItem bind and just use it on your method:
and on your code:
My example there is a column with NAME "service_id". But you can use int32 column offset as well. There is even an
ItemArray
in the DataRowView TYPE to run and and down. SeeSystem.Data
namespace. Your Datagrid itemssource / context will impact the "objects" you see inside the Datagrid. But if you check in debug the types, then you can Cast them and use them.