uc71
uc71

Reputation: 53

DataGrid trailing space after last column

In a wpf datagrid if the datagrid is wider than the sum of the column widths, you get trailing space. By default, clicking in this area does not select the row nor does the selection row highlighting cover this area.

How can you register clicks from this area into selecting the appropriate row and also allow the selection row highlighting to extend into this area.

This question: WPF DataGrid full row selection is similar but I cannot add a dummy column nor set my column widths to *.

Upvotes: 2

Views: 991

Answers (1)

LPL
LPL

Reputation: 17063

<DataGrid Name="dg">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <EventSetter Event="MouseLeftButtonDown" Handler="DataGridRow_MouseLeftButtonDown" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="{StaticResource {x:Static SystemColors.HighlightBrushKey}}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

with this code behind

private void DataGridRow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    dg.SelectedIndex = (sender as DataGridRow).GetIndex();
}

should work.

Upvotes: 3

Related Questions