DataGrid row content vertical alignment

后端 未结 9 909
借酒劲吻你
借酒劲吻你 2020-12-01 00:13

I have a regular DataGrid from WPF 4.0 RTM, where I put data from a database. In order to make clean & light style of DataGrid I use a tall/high rows and by

相关标签:
9条回答
  • 2020-12-01 00:36

    Just if someone might need it as i did..

    To only affect a single column you could use the 'ElementStyle' Property:

    <DataGrid ItemsSource="{Binding}">
        <DataGrid.Resources>
            <Style x:Key="DataGridVerticalText" TargetType="TextBlock">
                <Setter Property="VerticalAlignment" Value="Center" />
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Header Title" Binding="{Binding}" ElementStyle="{StaticResource DataGridVerticalText}" />
        </DataGrid.Columns>
    </DataGrid>
    
    0 讨论(0)
  • 2020-12-01 00:40

    To set individual text alignments you can use:

    <DataGridTextColumn.ElementStyle>
       <Style TargetType="TextBlock">
           <Setter Property="TextAlignment" Value="Center" />
       </Style>
    </DataGridTextColumn.ElementStyle>
    
    0 讨论(0)
  • 2020-12-01 00:43

    The following code will vertically align the content of a DataGridTextColumn cell:

    <DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
        </Style>
    </DataGridTextColumn.ElementStyle>
    

    Edit: I've come back to this problem and found the solution below to work better, it will center the contents of all the cells in DataGridTextRows both horizontally and vertically.

    <UserControl.Resources>    
        <ResourceDictionary>
            <Style TargetType="DataGridCell">
                <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
                <Setter Property="VerticalAlignment" Value="Stretch"></Setter>
                <Setter Property="VerticalContentAlignment" Value="Stretch"></Setter>
                <Setter Property="TextBlock.TextAlignment" Value="Center"></Setter>
                <Setter Property="TextBlock.VerticalAlignment" Value="Center"></Setter>
            </Style>    
        </ResourceDictionary>
    </UserControl.Resources>
    
    0 讨论(0)
提交回复
热议问题