如何将不同的样式应用于数据网格列

时间:2016-06-27 13:34:37

标签: wpf datagrid styles controltemplate

我对WPF很陌生,我不知道如何为数据网格的列使用两个不同的控件模板(总是有2列)。 这是DataGrid的XAML:

<DataGrid x:Name="HomeSoftwareGrid"
          CanUserAddRows="false"
          ItemsSource="{Binding CollectedSoftwares}"
          AutoGenerateColumns="True"
          FontSize="15"
          ColumnWidth="*"
          IsReadOnly="True"
          AutoGeneratingColumn="OnAutoGeneratingColumn"
          CellEditEnding="OnCellEditEnding"
          HorizontalAlignment="Center"
          MaxWidth="600">
</DataGrid>

我使用属性AutoGeneratingColumn删除特定列并编辑列&#39;头

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    PropertyDescriptor propertyDescriptor = (PropertyDescriptor)e.PropertyDescriptor;
    e.Column.Header = propertyDescriptor.DisplayName;
    if (propertyDescriptor.DisplayName == "Resources")
    {
        e.Cancel = true;
    }
    else if (propertyDescriptor.DisplayName == "SoftwareStatus")
    {
        e.Column.Header = "Software Status";

    }
    else if (propertyDescriptor.DisplayName == "SoftwareName")
    {
        e.Column.Header = "Software Name";
    }
}

这些是我想要使用的内容模板,第一列是第一列,第二列是第二列:D:

<!-- first column style -->
<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- second column style -->
<Style x:Key="SoftwareStatusDataGridColumn" TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid HorizontalAlignment="Center">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Rectangle x:Name="ImgPartially" Grid.Column="0"  Width="20" Height="20" Fill="Yellow">
                        <Rectangle.OpacityMask>
                            <VisualBrush Stretch="Uniform" Visual="{StaticResource appbar_warning}" />
                        </Rectangle.OpacityMask>
                    </Rectangle>

                    <ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我怎样才能实现这个目标?

1 个答案:

答案 0 :(得分:1)

事件的

sender是DataGrid,DataGrid可以在其可视化树中找到单元格样式。然后,您可以单独将该样式分配给列:

else if (propertyDescriptor.DisplayName == "SoftwareName")
{
    e.Column.Header = "Software Name";
    e.Column.CellStyle = (sender as FrameworkElement).FindResource("SoftwareStatusDataGridColumn") as Style;
}
<Style TargetType="{x:Type DataGridCell}">

此Style使用Type作为键,默认情况下将分配给DataGridCells,无需从后面的代码中明确设置