WPF datagrid text alignment in columns
-
Hi, i have a datagrid implemented in xaml like this:
The grid is automatically populated (via a Binding) with two columns and some data. All is well but the text in the columns is left-aligned. Is it possible to force the text in one of the autogenerated columns to be right-aligned? Regards
-
Hi, i have a datagrid implemented in xaml like this:
The grid is automatically populated (via a Binding) with two columns and some data. All is well but the text in the columns is left-aligned. Is it possible to force the text in one of the autogenerated columns to be right-aligned? Regards
You should be able to do this by applying a style to the DataGridColumnHeader. Off the top of my head, the following should do it:
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" HorizontalAlignment="Right" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
You should be able to do this by applying a style to the DataGridColumnHeader. Off the top of my head, the following should do it:
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding}" HorizontalAlignment="Right" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierHi and thanks for the reply. Unfortunately that did not do the trick. The column headers are hidden so i do not need to right-align them. Basically the datagrid has two columns of data. I need the DATA in ONE of the columns to be right aligned. I tried this:
<Setter Property="CellStyle"> <Setter.Value> <Style TargetType="DataGridCell"> <Setter Property="HorizontalAlignment" Value="Right"/>
but that just plain righ-aligns everything in the datagrid (data in column 1 and in column 2). Is it possible to apply this to column 2 only? Note that i do not have the column definitions in my xaml since the datagrid is populated via a binding. Thanks
-
Hi and thanks for the reply. Unfortunately that did not do the trick. The column headers are hidden so i do not need to right-align them. Basically the datagrid has two columns of data. I need the DATA in ONE of the columns to be right aligned. I tried this:
<Setter Property="CellStyle"> <Setter.Value> <Style TargetType="DataGridCell"> <Setter Property="HorizontalAlignment" Value="Right"/>
but that just plain righ-aligns everything in the datagrid (data in column 1 and in column 2). Is it possible to apply this to column 2 only? Note that i do not have the column definitions in my xaml since the datagrid is populated via a binding. Thanks
You can use the AutoGeneratingColumn event to handle this. Source[^].
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier