How to get WPF DataGrid cell for right alignment without making the selected area on a new line small?

I am using WPF4.0 DataGrid. Double-clicking on a cell in a new row works fine , unless I add a cell style to this column. For example, I have a numeric column where I want the data to be aligned right, so xaml looks like this:

<DataGridTextColumn Binding="{Binding Path=ImpaId}" CellStyle="{StaticResource CellRightAlign}"> <DataGridTextColumn.Header> <TextBlock Style="{StaticResource DataGridHeader}">Impa</TextBlock> </DataGridTextColumn.Header> </DataGridTextColumn> 

If the style in the share is simple:

 <Style x:Key="CellRightAlign"> <Setter Property="Control.HorizontalAlignment" Value="Right" /> </Style> 

The resulting selectable area in a new line is displayed on the image as this small blue area. This is a very small goal for the user who hits, and this is the most likely column with which they want to start a new line.

DataGrid cell with tiny width

If I delete CellStyle, the area will work as desired, but, of course, I will lose the correct alignment.

DataGrid cell with proper width

Does anyone know how to achieve both?

Things i tried

  • Setting TargetNullValue to bind to a format with a certain width. This works with existing lines, but does not affect the new line.
  • Setting MinWidth in the column, this did not affect the width of the selected row area.

The thing that worked:

Using the information from @AngelWPF's answer, I was able to switch from using CellStyle to using ElementStyle as follows:

 <DataGridTextColumn Binding="{Binding Path=ImpaId}" CellStyle="{StaticResource CellRightAlign}"> 

Has become

 <DataGridTextColumn Binding="{Binding Path=ImpaId}" ElementStyle="{StaticResource CellRightAlign}"> 
+57
wpf xaml datagrid
Oct 17 '11 at 22:30
source share
5 answers

You can apply an ElementStyle to a DataGridTextColumn targeting a TextBlock and right-aligned that this will 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> 
+83
18 Oct. '11 at 7:16
source share

Just wanted to add to other examples for future code searches.

I put this at the top of the xaml file:

 <UserControl.Resources> <Style TargetType="{x:Type TextBlock}" x:Key="RightCell"> <Setter Property="Background" Value="{Binding Included, Converter={StaticResource BoolToColorConverter}}"/> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="TextAlignment" Value="Right"/> </Style> </UserControl.Resources> 

And then in the datagrid:

 <DataGridTextColumn Header="Excluded" Binding="{Binding Excluded}" ElementStyle="{StaticResource RightCell}"/> 

This right aligns the text, and sorting is still enabled. The text box fills the cell and in this case is colored using the bool converter.

+14
Feb 24 '16 at 7:21
source share

You can try:

  <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Grid Background="White" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <TextBlock HorizontalAlignment="Right" Text="{Binding Path=ImpaId}"/> </Grid> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 
+5
Oct 17 2018-11-11T00:
source share

I had a similar problem and I found another solution by overwriting the style for the DataGridCell in Blend.

The modified elements of the original style are setters for VerticalAlignment and VerticalContentAlignment in the style itself to stretch the cell itself and VerticalAlignment="Center" and HorizontalAlignment="Right" in the Template property to align the contents. Change these values ​​to whatever you need to align the contents of the cells.

The rest of the style can be removed, so settings from the basic style will be used (using the StaticResource style - BasedOn). However, I left this style as Blend produced it.

As a result, this XAML code must be included in the management resources:

 <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource ResourceKey={x:Type DataGridCell}}"> <Setter Property="VerticalAlignment" Value="Stretch" /> <Setter Property="VerticalContentAlignment" Value="Stretch" /> <!-- Additional styles, can be removed to fall back to base styles --> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" HorizontalAlignment="Right" /> </Border> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger> <Trigger Property="IsKeyboardFocusWithin" Value="True"> <Setter Property="BorderBrush" Value="{DynamicResource {ComponentResourceKey ResourceId=FocusBorderBrushKey, TypeInTargetAssembly={x:Type DataGrid}}}"/> </Trigger> </Style.Triggers> </Style> 
+5
Jul 03 2018-12-12T00:
source share

I had the same problem and solved it now. If you want to do this inside C # code, you need to install several setters:

 DataGridTextColumn numColumn = new DataGridTextColumn Style s = new Style(); s.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Stretch)); s.Setters.Add(new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Right)); s.Setters.Add(new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)); s.Setters.Add(new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right)); numColumn.CellStyle = s; 
+1
Sep 19 '19 at 8:30
source share



All Articles