是否可以使DataGrid样式的Content Presenter因列而异?

时间:2020-08-20 22:24:43

标签: wpf datagrid wpf-controls

我继承了以下样式:

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

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

2 个答案:

答案 0 :(得分:0)

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

<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标记中。

注意:如果采用这种方式,则需要设置<DataGrid AutoGenerateColumns="False">

答案 1 :(得分:0)

如果您使用DataGrid及其内置列类型(如DataGridTextColumn)并希望更改对齐方式,则不必更改控件模板,只需创建样式并将其设置为{列中的{1}}。

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,但是对于像文本这样的简单数据,则不需要使用内置列。

相关问题