Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataGridView in my Project and I customize some of its features but I have a problem and my problem is I can't set cells content on center position.could you help me to fix this problem ?
( in fact I need a dark theme template for datagridview in wpf c# )

What I have tried:

<DataGrid x:Name="UsersInfoDataGridView" Grid.Column="0" Grid.Row="2" Width="800" Height="180" FlowDirection="RightToLeft" AutoGenerateColumns="False" BorderBrush="Transparent" Background="#FF4A4A4A" Foreground="White" HorizontalGridLinesBrush="#F6F7F9" VerticalGridLinesBrush="Transparent" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" RowHeaderWidth="10" RowHeight="30" RowBackground="#FF4A4A4A" Padding="5" AlternatingRowBackground="#FF4A4A4A">
                    <DataGrid.Columns>
                        <DataGridTextColumn Width="200" Header="نام و نام خانوادگی کاربر" Binding="{Binding UsersFullName}">
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="Background" Value="#333333"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="40"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Background" Value="#FF4A4A4A"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="30"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Width="200" Header="شماره تلفن" Binding="{Binding PhoneNumberD}">
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="Background" Value="#333333"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="40"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Background" Value="#FF4A4A4A"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="30"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Width="200" Header="ایمیل کاربر" Binding="{Binding EmailAddressD}">
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="Background" Value="#333333"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="40"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Background" Value="#FF4A4A4A"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="30"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Width="200" Header="نام کاربری کاربر" Binding="{Binding UserNameD}">
                            <DataGridTextColumn.HeaderStyle>
                                <Style TargetType="DataGridColumnHeader">
                                    <Setter Property="Background" Value="#333333"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="40"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.HeaderStyle>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Background" Value="#FF4A4A4A"/>
                                    <Setter Property="Foreground" Value="White"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="Width" Value="200"/>
                                    <Setter Property="Height" Value="30"/>
                                    <Setter Property="FontFamily" Value="Tahoma"/>
                                    <Setter Property="FontSize" Value="15"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>
Posted
Updated 24-May-19 8:30am

1 solution

There's another "alignment" property dealing with the actual "text" versus the "control's" placement.
C#
...TextAlignment = TextAlignment.Center;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900