[英]Text alignment in a WPF DataGrid
如何将列数据对齐到 WPF DataGrid
中心?
如果您使用的是 DataGridTextColumn,则可以使用以下代码片段:
<Style TargetType="DataGridCell">
<Style.Setters>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style.Setters>
</Style>
在不知道具体情况的情况下很难说,但这里有一个居中的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>
我从 httelihut 的解决方案开始。 不幸的是,这对我来说还没有用。 我调整了他的答案并想出了这个(解决方案是将文本向右对齐):
<Resources>
<Style x:Key="RightAligned" TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"/>
</Style>
</Resources>
如您所见,我将样式应用于 TextBlock,而不是 DataGridCell。
然后我必须设置元素样式,而不是单元样式。
ElementStyle="{StaticResource RightAligned}"
+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
,请设置DataGrid
的CellStyle
属性,例如
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 方法,但是当您尝试突出显示行时出现了奇怪的行为。
我在这个线程中放了另一个 WPF Datagrid 对齐的例子!
对我来说,这个效果很好
<DataGridTextColumn Width="1*" Binding="{Binding Balance, StringFormat=C} "Header="Balance">
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
我最喜欢的解决方案是:
<DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.CellStyle>
感谢 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
});
我真的很喜欢布鲁诺的 TextBlock.TextAlignment 方法。 您可以将其与水平对齐结合使用,然后任何背景都将横跨整个网格单元格。
例如(在VB中)
Dim sty = New System.Windows.Style(GetType(DataGridCell))
sty.Setters.Add(New Setter(HorizontalAlignmentProperty, HorizontalAlignment.Stretch))
sty.Setters.Add(New Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right))
sty.Setters.Add(New Setter(BackgroundProperty, Brushes.LightGray))
如果有人仍在为此寻找答案,这对我有用:
<DataGridTextColumn ...>
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Left"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.