C# WPF DataGrid 中的文本对齐方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720732/
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 a WPF DataGrid
提问by Prashant Cholachagudda
How can I align the column data to center in a WPF DataGrid
?
如何将列数据对齐到 WPF 的中心DataGrid
?
采纳答案by Kent Boogaart
It's hard to say without knowing specifics, but here's a DataGridTextColumn
that is centered:
在不知道具体情况的情况下很难说,但这里有一个DataGridTextColumn
集中的:
<wpf:DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True">
<wpf:DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
</wpf:DataGridTextColumn.CellStyle>
</wpf:DataGridTextColumn>
回答by Junior Mayhé
回答by Mohammed A. Fadil
If you are using DataGridTextColumn you can use the following code snippet:
如果您使用的是 DataGridTextColumn,则可以使用以下代码片段:
<Style TargetType="DataGridCell">
<Style.Setters>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style.Setters>
</Style>
回答by T.J.Kjaer
+1 for Kent Boogaart. I ended up doing this, which makes the code slightly less cluttered (and enables me to use the alignment on several columns):
+1 为肯特·布加特。我最终这样做了,这使代码稍微不那么混乱(并使我能够在多列上使用对齐方式):
<Resources>
<Style x:Key="NameCellStyle" TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Center" />
</Style>
</Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" CellStyle="{StaticResource NameCellStyle}" Binding="{Binding Name}"/>
// .. other columns
</DataGrid.Columns>
回答by Web Developer
My favorite solution is:
我最喜欢的解决方案是:
<DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.CellStyle>
回答by Hans
Or in code behind:
或者在后面的代码中:
grid.CellStyle = newCellStyle();
public static Style newCellStyle()
{
//And here is the C# code to achieve the above
System.Windows.Style style = new Style(typeof(DataGridCell));
style.Setters.Add(new System.Windows.Setter
{
Property = Control.HorizontalAlignmentProperty,
Value = HorizontalAlignment.Center
});
return style;
}
回答by Jan
I started with huttelihut's solution. Unfortunately, that didn't work for me just yet. I tweaked his answer and came up with this (solution is to align the text to the right):
我从 httelihut 的解决方案开始。不幸的是,这对我来说还行不通。我调整了他的答案并想出了这个(解决方案是将文本向右对齐):
<Resources>
<Style x:Key="RightAligned" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</Resources>
As you can see, I applied the style to a TextBlock, not the DataGridCell.
如您所见,我将样式应用于 TextBlock,而不是 DataGridCell。
And then I had to set the Elementstyle, not the Cellstyle.
然后我必须设置元素样式,而不是单元样式。
ElementStyle="{StaticResource RightAligned}"
回答by Rachael
I ended up having problems with the cell being shifted and looking funky using the accepted answer. I know it's late, but hopefully my findings will help someone. I use:
我最终遇到了问题,使用已接受的答案将单元格移动并看起来很时髦。我知道已经晚了,但希望我的发现会对某人有所帮助。我用:
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
rather than the CellStyle.
而不是 CellStyle。
回答by Danny Beckett
Here's @MohammedAFadil's XAML answer, converted to C# code behind:
这是@MohammedAFadil 的 XAML 答案,转换为后面的 C# 代码:
var MyStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
To apply the Style
, set the CellStyle
property of the DataGrid
, e.g.
要应用Style
,请设置 的CellStyle
属性DataGrid
,例如
var MyGrid = new DataGrid() {
CellStyle = MyStyle
};
回答by Stephen Himes
Thanks Danny Beckett for converting @MohammedAFadil's XAML answer, converted to C# code. All of my datagrids are set up dynamically, so I can change anything, whenever.
感谢 Danny Beckett 将 @MohammedAFadil 的 XAML 答案转换为 C# 代码。我所有的数据网格都是动态设置的,因此我可以随时更改任何内容。
To set up an empty datagrid, with nothing in it and then just bind it to data, just take your datagrid.columns
要设置一个空的数据网格,其中没有任何内容,然后将其绑定到数据,只需获取 datagrid.columns
var centerTextSetter = new Style(typeof(DataGridCell))
{
Setters = { new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center) }
};
DgDbNames.Columns.Add(new DataGridTextColumn()
{
Header = "Db Name",
Binding = new System.Windows.Data.Binding("DbName"),
IsReadOnly = true,
Width = new DataGridLength(0.2, DataGridLengthUnitType.Star),
CellStyle = centerTextSetter
});