Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

wpf - Text alignment in DataGrid

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...