【问题标题】:Can I have a DataGrid Style's Content Presenter vary by column?我可以让 DataGrid 样式的 Content Presenter 因列而异吗?
【发布时间】: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 Horizo​​ntalAlignment="{TemplateBinding Horizo​​ntalAlignment}" VerticalAlignment="Center" />。两个答案都让我到了同一个地方,但是我通过那个人的工作学到了很多东西,所以我检查了那个答案。我是这方面的新手,所以如果我做错了下次让我知道。

标签: wpf datagrid wpf-controls


【解决方案1】:

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

<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 或任何其他控件。也许为每个文本框/文本块制作一个样式并将其作为静态资源放在该 xaml 标记中。

注意:如果这样做,你需要设置&lt;DataGrid AutoGenerateColumns="False"&gt;

【讨论】:

  • 我最后也需要更改标题,所以你的风格帮助我很快到达那里 - 谢谢!
【解决方案2】:

如果您使用带有内置列类型的DataGrid,例如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 仅适用于数据网格单元格的非编辑模式。如果您需要在编辑模式下更改对齐方式或其他属性,您也必须为 EditingElementStyle 创建样式。

如果您需要在列中包含自定义控件,您可以使用DataGridTemplateColumn 并创建自定义DataTemplate,但对于文本等简单数据,这是不必要的,请改用内置列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多