wpf DataGrid 中的文本对齐方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18177153/
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
Text alignment in DataGrid
提问by Babak.Abad
I'm programming by WPF. I need a way to make center content of cells, in DataGrid control. I use this code also:
我正在用 WPF 编程。我需要一种在 DataGrid 控件中制作单元格中心内容的方法。我也使用此代码:
<DataGrid x:Name="dg1" HorizontalAlignment="Left" Margin="10,10,0,0"
VerticalAlignment="Top" Height="360" Width="498"
FontFamily="2 Badr" FontSize="18"
AlternatingRowBackground="LightCoral" FlowDirection="RightToLeft"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
Background="{x:Null}"/>
What is wrong?
怎么了?
回答by Lai32290
You need set DataGridCell style
您需要设置 DataGridCell 样式
<DataGrid>
<DataGrid.Columns>
<DataGridTextColumn>
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
回答by Fran?ois Cusson-Lafrenaye
For those who need to format only one dynamic DataGrid column in VB.NET from a custom XAML style:
对于那些只需要从自定义 XAML 样式格式化 VB.NET 中的一个动态 DataGrid 列的人:
In Application.xaml:
在 Application.xaml 中:
<Application.Resources>
<ResourceDictionary>
<Style x:Key="DataGridCellCentered" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
</ResourceDictionary>
</Application.Resources>
In VB.NET code:
在 VB.NET 代码中:
Me.MyDataGrid.Columns(5).CellStyle = TryFindResource("DataGridCellCentered")
Regards!
问候!
回答by flux
回答by Daniel
Maybe just create a style:
也许只是创建一个样式:
<Window.Resources>
<Style TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Window.Resources>
Edited.
已编辑。
回答by Mehdi Hamin
for affect all column
影响所有列
<Window.Resources>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>
</Window.Resources>
回答by daniele3004
In case you want to center the dates in a DataGridTemplateColumn
如果您想在 DataGridTemplateColumn 中将日期居中
<DataGridTemplateColumn SortMemberPath="DataDiNascita" Header="Data di nascita" IsReadOnly="False">
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<DatePicker SelectedDate="{Binding Path=DataDiNascita,Mode=TwoWay}" VerticalAlignment="Center" HorizontalAlignment="Left">
</DatePicker>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DataDiNascita,Mode=TwoWay,StringFormat=\{0:dd/MM/yyyy\}}" VerticalAlignment="Center" HorizontalAlignment="Left">
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
回答by yamen
How to center text in WPF DataGrid
如何在 WPF DataGrid 中居中文本
<DataGrid >
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
</DataGrid.CellStyle>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="Label.HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>