DataGrid行内容的垂直对齐方式 [英] DataGrid row content vertical alignment
问题描述
我从WPF 4.0 RTM,这里我把数据从数据库中一个普通的DataGrid。为了使清洁和放大器; DataGrid中的光的风格我用一个高大/高行,默认情况下的DataGrid对齐在顶部的垂直位置的行内容,但我想设置一个中心垂直对齐方式。
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 default DataGrid aligns row content in top vertical position, but I want to set a center vertical alignment.
我已经尝试过使用这个属性。
I already tried to use this property
VerticalAlignment="Center"
在DataGrid中选择,但它并不能帮助我。
in DataGrid options, but it doesn't help me.
下面是XAML- code的例子,描述我的DataGrid无中心垂直对齐方式:
Here is an example of XAML-code, describing my DataGrid without center vertical alignment:
<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>
执行此code的结果:
Result of executing this code:
正如你可以看到所有行的内容具有顶部垂直对齐。
As you can see all row's content has top vertical align.
我有什么,以便让各行内容的中心垂直对齐方式补充的吗?
What do I have to add in order to get center vertical alignment of each row content?
感谢。
推荐答案
这个问题的完整的解决方案:
Complete solution of this issue:
<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8c2aa3b1-d967-41ab-93c2-6c8cb1b7d29d/#488d0cdd-d0c2-469a-bfb6-8ca3d75426d4\">http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/8c2aa3b1-d967-41ab-93c2-6c8cb1b7d29d/#488d0cdd-d0c2-469a-bfb6-8ca3d75426d4
在短暂的,在风格文件集:
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>
在窗口中的文件:
<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:
这篇关于DataGrid行内容的垂直对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!