[英]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.