How can I align the column data to center in a WPF DataGrid
?
如何将列数据对齐到WPF DataGrid中?
10 个解决方案
#1
41
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>
#2
77
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>
#3
16
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):
我从huttelihut的解决方案开始。不幸的是,这对我还不起作用。我调整了他的答案,想出了这个(解决方案是将文本对齐到右边):
<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 Element style, not the Cell style.
然后我需要设置元素样式,而不是单元格样式。
ElementStyle="{StaticResource RightAligned}"
#4
12
+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):
肯特Boogaart + 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>
#5
7
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.
要应用样式,请设置DataGrid的CellStyle属性,例如。
var MyGrid = new DataGrid() {
CellStyle = MyStyle
};
#6
4
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;
}
#7
3
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。
#8
1
Ok, I used the frameworkElement approach but there was a strange behavior when you try to highlight the row.
好的,我使用了frameworkElement方法,但是当您试图突出显示行时出现了一个奇怪的行为。
I've put another example of WPF Datagrid alignment in this thread!
我在这个线程中放了另一个WPF Datagrid对齐的例子!
#9
1
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>
#10
0
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#代码。我的所有datagrid都是动态设置的,所以我可以随时更改任何内容。
To set up an empty datagrid, with nothing in it and then just bind it to data, just take your datagrid.columns
要设置一个空的datagrid,其中没有任何内容,然后将它绑定到数据,只需使用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
});
#1
41
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>
#2
77
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>
#3
16
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):
我从huttelihut的解决方案开始。不幸的是,这对我还不起作用。我调整了他的答案,想出了这个(解决方案是将文本对齐到右边):
<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 Element style, not the Cell style.
然后我需要设置元素样式,而不是单元格样式。
ElementStyle="{StaticResource RightAligned}"
#4
12
+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):
肯特Boogaart + 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>
#5
7
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.
要应用样式,请设置DataGrid的CellStyle属性,例如。
var MyGrid = new DataGrid() {
CellStyle = MyStyle
};
#6
4
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;
}
#7
3
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。
#8
1
Ok, I used the frameworkElement approach but there was a strange behavior when you try to highlight the row.
好的,我使用了frameworkElement方法,但是当您试图突出显示行时出现了一个奇怪的行为。
I've put another example of WPF Datagrid alignment in this thread!
我在这个线程中放了另一个WPF Datagrid对齐的例子!
#9
1
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>
#10
0
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#代码。我的所有datagrid都是动态设置的,所以我可以随时更改任何内容。
To set up an empty datagrid, with nothing in it and then just bind it to data, just take your datagrid.columns
要设置一个空的datagrid,其中没有任何内容,然后将它绑定到数据,只需使用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
});