如何将列数据与WPF DataGrid
中心对齐?
如果不知道细节,很难说,但这里是一个居中的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>
感谢Danny Beckett转换@ MohammedAFadil的XAML答案,转换为C#代码。我的所有数据网格都是动态设置的,所以我可以随时更改任何内容。
要设置一个空的数据网格,其中没有任何内容,然后将其绑定到数据,只需要使用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
});
对我来说这个很好用
<DataGridTextColumn Width="1*" Binding="{Binding Balance, StringFormat=C} "Header="Balance">
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
如果您使用的是DataGridTextColumn,则可以使用以下代码段:
<Style TargetType="DataGridCell">
<Style.Setters>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style.Setters>
</Style>
我从huttelihut的解决方案开始。不幸的是,这对我来说还不适用。我调整了他的答案并想出了这个(解决方案是将文本对齐):
<Resources>
<Style x:Key="RightAligned" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</Resources>
如您所见,我将样式应用于TextBlock,而不是DataGridCell。
然后我必须设置元素样式,而不是单元格样式。
ElementStyle="{StaticResource RightAligned}"
为Kent 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>
这是@ MohammedAFadil的XAML答案,转换为C#代码:
var MyStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
要应用Style
,请设置CellStyle
的DataGrid
属性,例如
var MyGrid = new DataGrid() {
CellStyle = MyStyle
};
或者代码背后:
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;
}
我最终遇到了细胞被移位的问题,并且使用接受的答案看起来很时髦。我知道现在已经很晚了,但希望我的发现可以帮助别人。我用:
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
而不是CellStyle。
好吧,我使用了frameworkElement方法,但是当你试图突出显示该行时,会出现一种奇怪的行为。
我在这个thread中添加了WPF Datagrid对齐的另一个例子!
我最喜欢的解决方案:
<DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.CellStyle>