1

I am looking for some help on WPF UI.

  1. I would like to have my data grid's content all to be shown in center of the column.

  2. In order to look nice in UI, I need the datagridtextboxcolumn and datagridtemplatecolumn (combo box) width and height fits the columns.

Currently, I already implement the style of Content Presenter with Stretch VerticalAlignment and HorizontalAlignment which success make the data fits to the columns width and height.

And it success showing the data horizontally center after I set setter property of TextBlock.TextAlignment.

But the data is not showing in the center vertically even though I already set the setter property of Vertical Content Alignment to Center.

So, I need experts to give me some advices on making the data shown in center vertically and horizontally with stretching the width height to fits the columns.

Below is my style code

    <Style x:Key="Body_Content_DataGrid_Centering"
    TargetType="{x:Type DataGridCell}">
        <Setter Property="TextBlock.TextAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

And below will be my DataGrid implementation code:

    <DataGrid x:Name="dataGridAccount" RowHeight="27" MinRowHeight="50" ColumnHeaderHeight="50" MinColumnWidth="150" RowHeaderWidth="0" Margin="0" Grid.Row="1" AutoGenerateColumns="False" CellStyle="{StaticResource Body_Content_DataGrid_Centering}" VerticalContentAlignment="Bottom">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding username}" ClipboardContentBinding="{x:Null}" Header="Username" Width="1*">
                <DataGridTextColumn.HeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    </Style>
                </DataGridTextColumn.HeaderStyle>
            </DataGridTextColumn>
            <DataGridTemplateColumn Header="Entry Level" Width="1*">
                <DataGridTemplateColumn.HeaderStyle>
                    <Style TargetType="{x:Type DataGridColumnHeader}">
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    </Style>
                </DataGridTemplateColumn.HeaderStyle>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding entryLevel}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                     <DataTemplate>
                        <ComboBox ItemsSource="{StaticResource entryLevel}"
                                                  SelectedItem="{Binding entryLevel}" Margin="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
                     </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

Current Result: https://i.sstatic.net/reRHc.jpg

Expected Result: https://i.sstatic.net/ag13j.jpg

2 Answers 2

0

I would suppose, that your Textblock inside the GridCells are not taking the whole space of the cell. You set the VerticalAlignment="Stretch" HorizontalAlignment="Stretch" for the GridCell, but not for the TextBlock inside this Cell. So you should add both Alignments to the TextBlock Style inside the Cells as well.

Sign up to request clarification or add additional context in comments.

1 Comment

But I can see that the Textblock had already take the effect of Stretch for VerticalAlignment and HorizontalAligment as shown in imgur.com/a/dGVWw Just I wonder why the VerticalContentAlignment does not effect on textblock as u can see the text is always on the top.
0

Try setting VerticalAlignment={TemplateBinding VerticalContentAlignment} on your ContentPresenter

2 Comments

Hi, the code works on the textblock which shown in center but it caused the combobox not stretch to fits column anymore. Any advices? Thanks
Advices? No. Workaround, yes. Try setting RowHeight of your DataGrid to a certain value, then set Height of your ComboBox in the cell to the same value. I'd love to help you do this in a proper way, to style everything in one place and apply it to a cell as a template, but as far as I know, there is no such possibility in DataGrids. I have been struggling with this just lately and found out that many parts of DataGrids can't be styled from ContentTemplate, because they don't inherit from Control class. Basically styling DataGrid is pain in the ass and you will have to do workarounds anyway...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.