【发布时间】:2020-12-10 07:29:29
【问题描述】:
我继承了以下样式:
<Style x:Key="MainPlanDataGridCell" TargetType="DataGridCell">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Height" Value="30" />
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
...
</Style>
<Style x:Key="MainPlanTable" TargetType="{x:Type DataGrid}">
...
<Setter Property="CellStyle" Value="{StaticResource MainPlanDataGridCell}" />
...
</Style>
它在控件中的使用是这样的:
<DataGrid
Grid.Row="2"
Grid.Column="0"
...
Style="{StaticResource MainPlanTable}">
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
但我需要让不同的单元格列具有不同的对齐方式。 有没有办法使用样式来实现这一点?如果没有,有人可以提出实现这一目标的最佳方法(高级方法)吗?
【问题讨论】:
-
感谢 thatguy 和 Bandook;两个人都回答了我。 @thatguy,我没有提到我遗漏了一大堆控制模板,并且它是程序一致性所必需的。在使用您的任何一种方法后,我最终添加的是向控件添加 TemplateBinding:ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="Center" />。两个答案都让我到了同一个地方,但是我通过那个人的工作学到了很多东西,所以我检查了那个答案。我是这方面的新手,所以如果我做错了下次让我知道。
标签: wpf datagrid wpf-controls