I'm using a WPF4.0 DataGrid. When double clicking on a cell in a new row everything works fine unless I've added a cell style to that column. For example, I have a numeric column where I want the data right aligned so the xaml looks like this
<DataGridTextColumn Binding="{Binding Path=ImpaId}"
CellStyle="{StaticResource CellRightAlign}">
<DataGridTextColumn.Header>
<TextBlock Style="{StaticResource DataGridHeader}">Impa</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>
Where the style in a shared resource is just:
<Style x:Key="CellRightAlign">
<Setter Property="Control.HorizontalAlignment"
Value="Right" />
</Style>
The resulting selectable area on a new row is shown in the image as that small blue area.This is a very small target for the user to hit, and this happens to be the most likely column they will want to start with on a new row.
If I remove the CellStyle, the area works as desired but of course I lose the right alignment.
Does anyone know how to achieve both?
Things I tried
Thing that worked:
Using the information from the answer by @AngelWPF I was able to change from using CellStyle to using ElementStyle as follows:
<DataGridTextColumn Binding="{Binding Path=ImpaId}"
CellStyle="{StaticResource CellRightAlign}">
Became
<DataGridTextColumn Binding="{Binding Path=ImpaId}"
ElementStyle="{StaticResource CellRightAlign}">
You could apply ElementStyle
on the DataGridTextColumn
targetted to TextBlock
and right align that, it would work.
<DataGridTextColumn Binding="{Binding Path=ImpaId}">
<DataGridTextColumn.Header>
<TextBlock Style="{StaticResource
DataGridHeader}">
Impa
</TextBlock>
</DataGridTextColumn.Header>
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="HorizontalAlignment" Value="Right" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>