I have a DataGrid with a RowDetailsTemplate containing another grid.
I want to react on a doubleclick on a row in that detailgrid and fill the cell's content into a corresponding cell of the selected parent row.
<DataGrid Name="dataGrid1" DataContext="{Binding}" ItemsSource="{Binding Source={StaticResource ..}}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Old Link Source" Binding="{Binding Path=OldLinkSource}"/>
<DataGridTextColumn Header="New Link Source" Binding="{Binding Path=NewLinkSource}"/>
</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid Name="dataGrid1Details" ItemsSource="{Binding Path=PossibleCandidates}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Similarity" Binding="{Binding Path=Key}"/>
<DataGridTextColumn Header="Possible New Link Source" Binding="{Binding Path=Value}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
<DataGrid.RowDetailsTemplate>
</DataGrid>
From my understanding the detailgrid is recreated every time the row is changed. I'm new to WPF and clueless on how to get grip of the currently visible detailgrid and subscribe to its events.
You can add a Style for DataGridRow inside the RowDetails DataGrid and subscribe to the MouseDoubleClick event from there.
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<DataGrid Name="dataGrid1Details" ItemsSource="{Binding Path=PossibleCandidates}" AutoGenerateColumns="False">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDoubleClick" Handler="DetailedDataGridRow_MouseDoubleClick"/>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Similarity" Binding="{Binding Path=Key}"/>
<DataGridTextColumn Header="Possible New Link Source" Binding="{Binding Path=Value}"/>
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
Code behind, simple EventHandler
// Fill cell data.. You can access the values like this
void DetailedDataGridRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
DataGridRow clickedDataGridRow = sender as DataGridRow;
// Details: clickedDataGridRow.Item
// Main DataGrid: dataGrid1.SelectedItem
}
Update
The RowDetails and the DataGridRow are connected, sort of. The RowDetails is in the DataGridRow in the VisualTree so there's many ways to access it (events, walking VisualTree etc.) but I don't think there's a Property or something like that that'll give you direct access (as far as I know). Screenshot from Snoop showing the DataGridDetailsPresenter in a DataGridRow