DataGrid control in WPF is behaving weird. When I click on a row it should be selected.
There is a problem when I click on cell border or row border. It simply does nothing. As a user I want to click row and select it without forcing me to re-click cause I accidentally clicked on border between cells.
Is it possible to somehow fix this behavior so no matter where I click it selects row?
[edit]
I discovered its a matter of this style I applied to DataGrid to CellStyle property:
<Style x:Key="CustomDataGridCellStyle" TargetType="DataGridCell">
<Setter Property="Padding" Value="2" />
<Setter Property="Margin" Value="0" />
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<Border x:Name="border"
BorderBrush="#CCaaccda"
BorderThickness="1"
CornerRadius="0"
Padding="4"
>
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsSelected}" Value="true">
<Setter TargetName="border" Property="Background" Value="#CC119EDA"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If I remove it its working. I wander how style can mess with interactions to control.
[edit]
Padding="4"
is preventing the created empty area between cells to not be able to take hit test. Any idea how to add not hittest blocking padding to cells?
[edit]
And the solution is very weird.
<Grid Background="Transparent" IsHitTestVisible="True">
<ContentPresenter Margin="4"/>
</Grid>
Margin is added to cell content and its not reacting to click. But I surrounded it with Grid that have IsHitTestVisible="True" and is transparent. WPF is crazy weird.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…