DataGrid row content vertical alignment

后端 未结 9 908
借酒劲吻你
借酒劲吻你 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:21

    This one works for me

     <DataGrid.CellStyle>
       <Style TargetType="DataGridCell">              
         <Setter Property="TextBlock.TextAlignment" Value="Center"/>
         <Setter Property="Template">
           <Setter.Value>
             <ControlTemplate TargetType="{x:Type DataGridCell}">
               <Grid Background="{TemplateBinding Background}">
                 <ContentPresenter VerticalAlignment="Center"/>
               </Grid>
             </ControlTemplate>
           </Setter.Value>
         </Setter>
       </Style>
    </DataGrid.CellStyle>
    
    0 讨论(0)
  • 2020-12-01 00:22

    This is my simple solution and it just works perfectly

    <DataGridTemplateColumn Header="Hello" Width="200">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="World!" TextAlignment="Center" VerticalAlignment="Center"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    I set the width to 200 so you can notice the difference.

    0 讨论(0)
  • 2020-12-01 00:26

    Complete solution of this issue at MSDN: Vertical alignment of DataGrid row content.

    In brief, in style-file set:

    <!--body content datagrid cell vertical centering-->
    <Style x:Key="Body_Content_DataGrid_Centering"
            TargetType="{x:Type DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    In window file:

    <DataGrid x:Name="ContentDataGrid"
            Style="{StaticResource ContentDataGrid}"
            CellStyle="{StaticResource Body_Content_DataGrid_Centering}"
            ItemsSource="{Binding}"
            RowEditEnding="ContentDataGrid_RowEditEnding">
        <DataGrid.Columns>
            <DataGridTextColumn Header="UserID"
                    Width="100"
                    IsReadOnly="True"
                    Binding="{Binding Path=userID}" />
            <DataGridTextColumn Header="UserName"
                    Width="100"
                    Binding="{Binding Path=userName}" />
            <DataGridTextColumn Header="UserAccessLevel"
                    Width="100"
                    Binding="{Binding Path=userAccessLevel}" />
            <DataGridTextColumn Header="UserPassword"
                    Width="*"
                    Binding="{Binding Path=userPassword}" />
        </DataGrid.Columns>
    </DataGrid>
    

    This will give you a wanted result:

    alt text

    0 讨论(0)
  • 2020-12-01 00:27

    Building on Jamier's answer, the following code did the trick for me when using auto-generated columns:

    Style VerticalCenterStyle = new Style();
    
    public MainWindow()
    {
      // This call is required by the designer.
      InitializeComponent();
    
      VerticalCenterStyle.Setters.Add(new Setter(VerticalAlignmentProperty, VerticalAlignment.Center));
    }
    
    private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {   
      if (e.Column is DataGridTextColumn) {
        ((DataGridTextColumn)e.Column).ElementStyle = VerticalCenterStyle;
      }
    }
    
    0 讨论(0)
  • 2020-12-01 00:28

    You could also do without overriding the ControlTemplate:

        <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="VerticalAlignment" Value="Center" />
        </Style>
    
    0 讨论(0)
  • 2020-12-01 00:33

    The attribute value VerticalAlignment="Center" will center the DataGrid within its parent element.

    You probably want VerticalContentAlignment.

    0 讨论(0)
提交回复
热议问题