Reputation: 2956
I'm programming by WPF. I need a way to make center content of cells, in DataGrid control. I use this code also:
<DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="10,10,0,0"
VerticalAlignment="Top" Height="360" Width="498"
FontFamily="2 Badr" FontSize="18"
AlternatingRowBackground="LightCoral" FlowDirection="RightToLeft"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Background="{x:Null}"/>
What is wrong?
Upvotes: 65
Views: 115060
Reputation: 150
This may be a more simpler solution.
<DataGrid>
<!-- your code -->
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataGrid.CellStyle>
</DataGrid>
Upvotes: 0
Reputation: 161
In my case this (very simple) is working fine:
<DataGrid
x:Name="MyDataGrid"
...
TextBlock.TextAlignment="Center">
</DataGrid>
Upvotes: 2
Reputation: 31
I think it's depends on panel who is filled or docked in datagrid horizontal content alignment doesn't work for center content alignment you can use DataGrid.resource tag like this
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</DataGrid.Resources>
or see this one
<DataGrid x:Name="DgvUsers" AutoGenerateColumns="False" IsReadOnly="True" EnableRowVirtualization="False"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" HeadersVisibility="Column"
IsTextSearchEnabled="True" FlowDirection="RightToLeft" SelectionMode="Single" FontFamily="/Sandogh.App;component/Font/#Lalezar" >
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="UserID" Visibility="Collapsed" Binding="{Binding Path=UserID}"/>
<DataGridTextColumn Header="FirstName" Binding="{Binding Path=Name}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Header="LastName" Binding="{Binding Path=Family}"/>
<DataGridTextColumn Header="Gender" Binding="{Binding Path=TGender}"/>
<DataGridTextColumn Header="UserName" Binding="{Binding Path=UserName}"/>
<DataGridTextColumn Header="Password" Binding="{Binding Path=Password}"/>
<DataGridTextColumn Header="Activity" Binding="{Binding Path=TActivity}"/>
<DataGridTextColumn Header="JobID" Visibility="Collapsed" Binding="{Binding Path=JobID}"/>
<DataGridTextColumn Header="JobName" Binding="{Binding Path=JobName}"/>
</DataGrid.Columns>
</DataGrid>
Upvotes: 0
Reputation: 19
How to center text in WPF DataGrid
<DataGrid >
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
</DataGrid.CellStyle>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Label.HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
Upvotes: 2
Reputation: 341
For those who need to format only one dynamic DataGrid column in VB.NET from a custom XAML style:
In Application.xaml:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="DataGridCellCentered" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
</ResourceDictionary>
</Application.Resources>
In VB.NET code:
Me.MyDataGrid.Columns(5).CellStyle = TryFindResource("DataGridCellCentered")
Regards!
Upvotes: 31
Reputation: 1648
As mentioned in other answers:
<Setter Property="HorizontalAlignment" Value="Center" />
This will affect any other styles such as background. To only center the text use this instead:
<Setter Property="TextAlignment" Value="Center" />
Upvotes: 16
Reputation: 934
for affect all column
<Window.Resources>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</Window.Resources>
Upvotes: 3
Reputation: 13960
In case you want to center the dates in a DataGridTemplateColumn
<DataGridTemplateColumn SortMemberPath="DataDiNascita" Header="Data di nascita" IsReadOnly="False">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=DataDiNascita,Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Left">
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DataDiNascita,Mode=TwoWay,StringFormat=\{0:dd/MM/yyyy\}}" VerticalAlignment="Center" HorizontalAlignment="Left">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Upvotes: 1
Reputation: 9521
Maybe just create a style:
<Window.Resources>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Window.Resources>
Edited.
Upvotes: 10
Reputation: 8578
You need set DataGridCell style
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Upvotes: 141