xaml 无法在 DataGrid 模板中垂直对齐文本
Posted
技术标签:
【中文标题】xaml 无法在 DataGrid 模板中垂直对齐文本【英文标题】:xaml Cannot align text vertically in in DataGrid template 【发布时间】:2015-12-29 05:09:04 【问题描述】:我正在尝试将 DataGrid 的所有单元格中的文本对齐到中心。我找到的有关如何执行此操作的所有示例都显示了较小的 DataGrid,其中有一个 ContentPresenter,它具有垂直对齐文本的属性,但是我的 DataGrid 样式没有 ContentPresenter。我试图通过创建一个来对齐
<Setter Property="VerticalAlignment" Value="Center"/>
在带有 TargetType x:Type DataGrid 的样式中。
Style的整个代码是这样的
<Style x:Key="myDataGrid" TargetType="x:Type DataGrid">
<Setter Property="AlternatingRowBackground" Value="WhiteSmoke"/>
<!-- <Setter Property="Background" Value="DynamicResource x:Static SystemColors.ControlBrushKey"/> -->
<Setter Property="Background" Value="DynamicResource grayBackground"/>
<Setter Property="RowBackground" Value="DynamicResource rowWhiteBackground"/>
<Setter Property="Foreground" Value="DynamicResource x:Static SystemColors.ControlTextBrushKey"/>
<Setter Property="BorderBrush" Value="DynamicResource standardRed"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FontFamily" Value="Helvetica"/>
<Setter Property="GridLinesVisibility" Value="Vertical"/>
<Setter Property="VerticalGridLinesBrush" Value="LightGray"/>
<Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected"/>
<Setter Property="MinRowHeight" Value="30"/>
<Setter Property="MinColumnWidth" Value="100"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="ScrollViewer.PanningMode" Value="Both"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type DataGrid">
<Border BorderBrush="TemplateBinding BorderBrush" BorderThickness="TemplateBinding BorderThickness" Background="TemplateBinding Background" Padding="TemplateBinding Padding" SnapsToDevicePixels="True">
<ScrollViewer x:Name="DG_ScrollViewer" Focusable="false">
<ScrollViewer.Template>
<ControlTemplate TargetType="x:Type ScrollViewer">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Button Command="x:Static DataGrid.SelectAllCommand" Focusable="false" Style="DynamicResource ComponentResourceKey ResourceId=DataGridSelectAllButtonStyle, TypeInTargetAssembly=x:Type DataGrid" Visibility="Binding HeadersVisibility, ConverterParameter=x:Static DataGridHeadersVisibility.All, Converter=x:Static DataGrid.HeadersVisibilityConverter, RelativeSource=RelativeSource AncestorType=x:Type DataGrid" Width="Binding CellsPanelHorizontalOffset, RelativeSource=RelativeSource AncestorType=x:Type DataGrid"/>
<DataGridColumnHeadersPresenter x:Name="PART_ColumnHeadersPresenter" Grid.Column="1" Visibility="Binding HeadersVisibility, ConverterParameter=x:Static DataGridHeadersVisibility.Column, Converter=x:Static DataGrid.HeadersVisibilityConverter, RelativeSource=RelativeSource AncestorType=x:Type DataGrid"/>
<ScrollContentPresenter x:Name="PART_ScrollContentPresenter" CanContentScroll="TemplateBinding CanContentScroll" Grid.ColumnSpan="2" Grid.Row="1"/>
<ScrollBar x:Name="PART_VerticalScrollBar" Grid.Column="2" Maximum="TemplateBinding ScrollableHeight" Orientation="Vertical" Grid.Row="1" Visibility="TemplateBinding ComputedVerticalScrollBarVisibility" Value="Binding VerticalOffset, Mode=OneWay, RelativeSource=RelativeSource TemplatedParent" ViewportSize="TemplateBinding ViewportHeight"/>
<Grid Grid.Column="1" Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Binding NonFrozenColumnsViewportHorizontalOffset, RelativeSource=RelativeSource AncestorType=x:Type DataGrid"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ScrollBar x:Name="PART_HorizontalScrollBar" Grid.Column="1" Maximum="TemplateBinding ScrollableWidth" Orientation="Horizontal" Visibility="TemplateBinding ComputedHorizontalScrollBarVisibility" Value="Binding HorizontalOffset, Mode=OneWay, RelativeSource=RelativeSource TemplatedParent" ViewportSize="TemplateBinding ViewportWidth"/>
</Grid>
</Grid>
</ControlTemplate>
</ScrollViewer.Template>
<ItemsPresenter SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels"/>
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsGrouping" Value="true"/>
<Condition Property="VirtualizingPanel.IsVirtualizingWhenGrouping" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
</MultiTrigger>
</Style.Triggers>
</Style>
代码取自我在 Blend 和 Visual Studio 中编辑的 DataGrid 样式的副本。我找不到垂直对齐所有单元格文本的位置或方式。感谢我能得到的所有帮助。
【问题讨论】:
【参考方案1】:这对你有用:
<Window.Resources>
<ControlTemplate x:Key="CustomCell" TargetType="DataGridCell">
<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Height="100">
<ContentPresenter Width="Auto" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Label>
</ControlTemplate>
<Style TargetType="DataGridCell">
<Setter Property="Template" Value="StaticResource CustomCell"/>
</Style>
...
</Window.Resources>
【讨论】:
【参考方案2】:试试这个。
<DataGrid.LayoutTransform>
<TransformGroup>
<RotateTransform Angle="90" />
<MatrixTransform Matrix="-1,0,0,1,0,0" />
</TransformGroup>
</DataGrid.LayoutTransform>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="x:Type DataGridColumnHeader"
BasedOn="StaticResource x:Type DataGridColumnHeader">
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<RotateTransform Angle="-90" />
<ScaleTransform ScaleX="1" ScaleY="-1" />
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<RotateTransform Angle="-90" />
<ScaleTransform ScaleX="1" ScaleY="-1" />
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
</DataGrid.CellStyle>
【讨论】:
以上是关于xaml 无法在 DataGrid 模板中垂直对齐文本的主要内容,如果未能解决你的问题,请参考以下文章