wpf 如何在datagrid WPF中制作中心列文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22342400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make center column text in datagrid WPF?
提问by mrhands
I have wpf datagrid with many columns in my datagrid..
我的数据网格中有许多列的 wpf 数据网格..
<DataGrid.Columns>
...
<mui:DataGridTextColumn x:Name="Column27" Width="50" Header="Cabe" Binding="{Binding B4R27,UpdateSourceTrigger=PropertyChanged ,Converter={StaticResource CheckConverter}, Mode=TwoWay}" />
<mui:DataGridTextColumn IsReadOnly="True" x:Name="Column28" Width="50" Header="Jumlah Bahan Pokok" Binding="{Binding B4RJ,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" >
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Background" Value="#A2D1A2" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>
</DataGridTextColumn.CellStyle>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</mui:DataGridTextColumn>
<mui:DataGridTextColumn x:Name="Column29" Width="150" Header="Tulis Nama Pengusaha" Binding="{Binding B4R28,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" />
<mui:DataGridTextColumn x:Name="Column30" Width="130" Header="Tulis Alamat Lengkap" Binding="{Binding B4R29,UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}" />
</DataGrid.Columns>
I can make center my text in datagrid using this style with this code above
我可以使用上面的代码使用这种样式使我的文本在数据网格中居中
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
But I want it in all my columns datagridtextcolumnin my datagrid.
How I can make it like style so all my datagridtextcolumnhas the same centered alignment text?
但我希望它出现datagridtextcolumn在我的数据网格中的所有列中。我怎样才能让它像风格一样,所以我所有的都datagridtextcolumn具有相同的居中对齐文本?
回答by Anatoliy Nikolaev
Try this Stylefor DataGridColumnHeader:
试试这个Style为DataGridColumnHeader:
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.Resources>
You can also put him in ColumnHeaderStyle:
你也可以把他放进去ColumnHeaderStyle:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
If you have the current Style for DataGridColumnHeader, then you need to use the style inheritance using BasedOnlike this:
如果您有当前的 Style for DataGridColumnHeader,那么您需要使用样式继承,BasedOn如下所示:
<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" <--- Here may also be the key of your Style
TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
If you want set content of DataGridCellto center, then use this Style:
如果要将内容设置DataGridCell为中心,请使用以下命令Style:
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

