我可以让 DataGrid Style 的 Content Presenter 因列而异吗?

Can I have a DataGrid Style's Content Presenter vary by column?

我继承了以下风格:

<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>

但我需要让不同的单元格列具有不同的对齐方式。 有没有办法使用样式来完成此操作?如果不能,有人可以建议实现此目标的最佳方法(高级方法)吗?

我为原型应用程序创建了类似的东西,如果可能,您可以提取样式。否则就这样使用它,

<DatGrid.Columns>
    <DataGridTemplateColumn Header="ColumnHeader1" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding something}" //your alignment & other styles here />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    <DataGridTemplateColumn Header="ColumnHeader2" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding somethingElse}" //your alignment & other styles here />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
...
</DataGrid.Columns>

您可以控制单元格是 TexBlock 还是 TextBox 或任何其他控件。也许为每个 textbox/textblock 制作一个样式,并将其作为静态资源放在 xaml 标签中。

注意:如果这样做,您需要设置 <DataGrid AutoGenerateColumns="False">

如果您使用 DataGrid 及其 built-in 列类型(例如 DataGridTextColumn)并且想要更改对齐方式,则不必更改控件模板,只需创建样式和将其设置为列的 ElementStyle

<!-- Example style that centers the text block in the column -->
<Style x:Key="TextBlockColumnAlignCenterStyle"
       TargetType="{x:Type TextBlock}"
       BasedOn="{StaticResource {x:Type TextBlock}}">
   <Setter Property="TextAlignment" Value="Center"/>
   <Setter Property="VerticalAlignment" Value="Center"/>
</Style>

TargetType 必须匹配列中的控件。这是 DataGridCheckBoxColumn.

的示例
<Style x:Key="CheckBoxColumnAlignCenterStyle"
       TargetType="{x:Type CheckBox}"
       BasedOn="{StaticResource {x:Type CheckBox}}">
   <Setter Property="HorizontalAlignment" Value="Center"/>
   <Setter Property="VerticalAlignment" Value="Center"/>
</Style>

要应用这些样式,只需将它们分配给 DataGrid 中的目标列,例如:

<DataGrid ItemsSource="{Binding MyItems}" AutoGenerateColumns="False">
   <!-- ...other code. -->
   <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding MyText}"
                          ElementStyle="{StaticResource TextBlockColumnAlignCenterStyle}"/>
      <DataGridCheckBoxColumn Binding="{Binding MyBoolean}"
                              ElementStyle="{StaticResource CheckBoxColumnBackgroundStyle}"/>
   </DataGrid.Columns>
</DataGrid>

请注意,ElementStyle 仅适用于数据网格单元格的 non-edit 模式。如果您需要在编辑模式下更改对齐方式或其他属性,您也必须为 EditingElementStyle 创建样式。

如果您需要在您的列中使用自定义控件,您可以使用 DataGridTemplateColumn 并创建自定义 DataTemplate,但对于像文本这样的简单数据,这是不必要的,请使用 built-in 列。