C# WPF数据网格中的文本对齐
如何将列数据与WPF中的中心对齐C# WPF数据网格中的文本对齐,c#,.net,wpf,xaml,wpftoolkit,C#,.net,Wpf,Xaml,Wpftoolkit,如何将列数据与WPF中的中心对齐DataGrid?不知道细节很难说,但这里有一个位于中心的DataGridTextColumn: <wpf:DataGridTextColumn Header="Name" Binding="{Binding Name}" IsReadOnly="True"> <wpf:DataGridTextColumn.CellStyle> <Style> <Setter Propert
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>
好的,我使用了frameworkElement方法,但是当您试图突出显示行时出现了一种奇怪的行为
我在这里又举了一个WPF数据网格对齐的例子 如果您使用的是DataGridTextColumn,则可以使用以下代码段:
<Style TargetType="DataGridCell">
<Style.Setters>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style.Setters>
</Style>
+1为肯特·布加特。
我最终这样做了,这使得代码稍微不那么凌乱(并使我能够在多个列上使用对齐方式):
// .. 其他栏目
我最喜欢的解决方案是:
<DataGridTextColumn Header="My Column" Binding="{Binding MyDBValue}" Width="100" >
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
</DataGridTextColumn.CellStyle>
或在代码隐藏中:
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;
}
我从huttelihut的解决方案开始。不幸的是,这对我来说还不起作用。我调整了他的答案,得出了以下结论(解决方案是将文本向右对齐):
我最终遇到了移动手机的问题,并且用公认的答案看起来很怪。我知道为时已晚,但希望我的发现能帮助一些人。我使用:
<DataGridTextColumn.ElementStyle>
<Style>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
</Style>
而不是囚室风格 以下是@MohammedAFadil的XAML答案,转换为C#代码隐藏:
var MyStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
要应用样式
,请设置数据网格
的单元格样式
属性,例如
var MyGrid = new DataGrid() {
CellStyle = MyStyle
};
感谢Danny Beckett将@MohammedAFadil的XAML答案转换为C代码。我所有的数据网格都是动态设置的,所以我可以随时更改任何内容 要设置一个空的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
});
对我来说,这个很好用
<DataGridTextColumn Width="1*" Binding="{Binding Balance, StringFormat=C} "Header="Balance">
<DataGridTextColumn.CellStyle>
<Style>
<Setter Property="TextBlock.TextAlignment" Value="Right"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
我非常喜欢布鲁诺的TextBlock.TextAlignment方法。您可以将其与水平对齐结合使用,然后任何背景都将延伸到整个网格单元 e、 g.(在VB中)
如果有人仍在寻找答案,以下是对我有效的方法:
<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>
即使在2010年12月,我的工作也非常出色!这种方法对我不起作用。文本居中,但单元格宽度不再与其标题匹配。穆罕默德的解决方案效果最好。正如基夫所说,细胞也在移动。您必须像Mohammed A.Fadil Example.+1那样将文本块作为样式的目标,但对我来说,HorizontalAlignment
不起作用,而HorizontalContentAlignment
起作用。您可以对DataGridColumnHeader样式执行相同的操作,并将其设置为HeaderStyle。很好的干净解决方案。对于Silverlight,请参阅。这是最好的方法-在单元格上设置对齐方式会导致单元格无法填充其宽度,这在最初选择行时看起来很奇怪。这对我来说不起作用,但在我的代码中进行了一些测试并修复了一些错误后,此解决方案非常有效。谢谢@BrianVPS欢迎,我很高兴听到:D@PriyankThakkar,欢迎:请参阅此版本的C#代码隐藏版本。简单明了+1:)正是我所需要的。请注意,这不适用于DataGridTextColumn
@JP Hellemons:对我来说,它适用于DataGridTextColumn
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>
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>