The XAML markup has a DataGrid that is partially filled with columns.
Behind the data columns there is an empty column that fills the remaining width of the DataGrid.
When the user clicks on a row in this space, the row is not selected.
Can this be changed somehow?
Clarification of the question in connection with the answer from @BionicCode.
An empty column is not a cell. This is understandable and does not raise questions.
BUT!!! It is a ROW!
It's easy enough to make sure of this if you set the processing of the row event (there is an example below).
The example given at the beginning of the question is a simplification of a real task to demonstrate the question.
In a real task, apart from the "last empty column", there is also a problem with the cells in which the margin for the DataGridCell is set.
In this case, there is a row background around the cell.
Select a line when clicking on this background does not work.
And in the selected line, this background is not highlighted.
Since this is a common problem with highlighting in an "empty column", I did not write about it in the question initially.
<Window x:Class="SelectRow.SelectRowWind"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SelectRow"
xmlns:spec="clr-namespace:System.Collections.Specialized;assembly=System"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
mc:Ignorable="d"
Title="SelectRowWind" Height="450" Width="800">
<Window.Resources>
<spec:StringCollection x:Key="source">
<sys:String>First</sys:String>
<sys:String>Second</sys:String>
</spec:StringCollection>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{DynamicResource source}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate DataType="{x:Type sys:String}">
<TextBlock Text="{Binding Length}"
Background="Aqua" Padding="5"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Margin" Value="10"/>
</Style>
</DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="OnMouseDoubleClick"/>
</Style>
</DataGrid.ItemContainerStyle>
</DataGrid>
</Grid>
<x:Code>
<![CDATA[
private void OnMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MessageBox.Show($"DoubleClick in Row: \"{((FrameworkElement)sender).DataContext}\"");
}
]]>
</x:Code>
</Window>


The highlighted empty space is not a real row or column in the sense that it is represented as
DataGridCellorDataGridColumn. The grid lines are rendered using theGrid.ShowGridLinesfeature.Of course, you could capture the mouse position and map it to a row, using the framework's hit-test API by calling
VisualTreeHelper.HitTest.Stretch the last column
A simpler solution would be to to allow the last column to occupy the remaining space by setting
DataGridColumn.Widthto*.If your columns are defined explicitly, then you can set it locally on the column element:
Otherwise you should handle the
DataGrid.AutoGeneratedColumnsevent:XAML
Code-behind
Alternatively override the default styles
This solution is not recommended as it requires the reimplemention of the selection behavior. Allowing the last column to stretch is far less complex and doesn't risk to break the default behavior.
DataGridRow click handler
DataGridRow Style
DataGridCell Style
Remarks
This implementation only targets the requirement to double click the non cell area to highlight the entire row. The select row on cell click behavior and the remaining sellection unit behaviors (
DataGridSelectionUnit.CellandDataGridSelectionUnit.CellOrRowHeader) are still missing and required in order to provide the default behavior.