2010-10-20 77 views
44

我有一个WPF 4.0 RTM常规DataGrid,我从数据库中放入数据。为了使清洁&光栅样式的DataGrid我使用高/高行,默认情况下DataGrid排列行垂直位置的内容,但我想设置一个中心垂直对齐。DataGrid行内容垂直对齐

我已经尝试过在DataGrid中选择使用此属性

VerticalAlignment="Center" 

,但它并不能帮助我。

这里是XAML代码的例子,说明我的DataGrid无中心垂直对齐方式:

alt text

正如你可以看到所有列的内容:

<DataGrid x:Name="ContentDataGrid" 
    Style="{StaticResource ContentDataGrid}" 
    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> 

这段代码执行的结果有顶部垂直对齐。

为了获得每行内容的中心垂直对齐,我必须添加什么?

谢谢。

+0

anot她的方法,使用DataGridColumn继承:http://blog.smoura.com/wpf-toolkit-datagrid-part-iii-playing-with-datagridcolumns-and-datagridcells/ - 允许简单地设置继承列上的VerticalAlignment属性 - 需要从每个使用的列类型继承,所以这是相当有限的。也许有人会把它翻译成行为? – surfen 2011-12-19 15:32:13

回答

85

这个问题的完整的解决方案:在风格文件集

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8c2aa3b1-d967-41ab-93c2-6c8cb1b7d29d/#488d0cdd-d0c2-469a-bfb6-8ca3d75426d4

简单地说,:

<!--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> 

在窗口中的文件:

<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> 

这会给你想要的结果:

alt text

+20

我知道这是一个古老的答案。但是,对于简单的内容定位来说,这不是很多工作吗? – neebz 2011-02-20 12:03:32

+1

不幸的是,我没有找到任何其他简单的解决方案,如果你有这样的,请分享你的知识。 – 2011-02-28 23:20:19

+0

主要原因是因为DataGrid包含DataGridRow谁包含DataGridCell谁Containt控件。你希望这些控件是垂直中心。这就是Toucki正在做的事情。 – 2011-06-08 20:54:19

40

要设置单独的文字对齐方式,你可以使用:

<DataGridTextColumn.ElementStyle> 
    <Style TargetType="TextBlock"> 
     <Setter Property="TextAlignment" Value="Center" /> 
    </Style> 
</DataGridTextColumn.ElementStyle> 
+9

是的它的工作原理(使用VerticalAlignment,因为TextAlignment是水平文本定位)必须为所有列设置元素样式(复选框列和组合列将需要不同的样式)。 – surfen 2011-12-18 17:58:00

+0

就像这样,因为如果启用了这些方法,它不会中断网格线。 – 2012-02-22 09:51:59

+0

如何在编辑时将文本居中? – Erik 2016-05-30 09:53:32

16

下面的代码将垂直对齐DataGridTextColumn单元格的内容:

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

编辑:我已经回到这个问题,并发现下面的解决方案更好地工作,它将中心水平和垂直DataGridTextRows中的所有单元格的内容。

<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> 
+1

不起作用。尝试设置DataGrid RowHeight = 100使行高于默认高度。网格线看起来很奇怪。你的第一个解决方案(DataGridTextColumn.ElementStyle)对我来说效果更好。 – joedotnot 2015-05-29 00:24:18

12

你也可以做不重写控件模板:

<Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="VerticalAlignment" Value="Center" /> 
    </Style> 
+1

不知道为什么这个答案没有任何投票。它比更复杂的答案简单得多,并且工作正常! – 2014-04-10 20:21:33

+4

只有当您没有垂直网格线时,此答案才有效。否则,如果设置了RowHeight属性,则网格线将会出现间隙。 – Sven 2014-09-01 17:29:22

1

大厦Jamier的答案,使用自动生成的列时,将下面的代码并获得成功对我来说:

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; 
    } 
} 
6

这一个适用于我

<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>