【问题标题】:How to apply different styles to datagrid columns如何将不同的样式应用于数据网格列
【发布时间】:2016-06-27 13:34:36
【问题描述】:

我对 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 删除特定列并编辑列的标题

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>

我怎样才能做到这一点?

【问题讨论】:

    标签: wpf datagrid styles controltemplate


    【解决方案1】:

    sender 的 event 是 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 作为 key,默认分配给 DataGridCells,无需在后面的代码中显式设置

    【讨论】:

    • 谢谢!这就像第二列的魅力,但似乎第一列“失去”了它的样式:文本应该居中对齐,但是当它在第二列上应用样式时,它变成左对齐:S
    • @Sempri92, &lt;Style TargetType="{x:Type DataGridCell}"&gt; 应该默认分配。它对我有用。但是您仍然可以使用显式键 &lt;Style x:Key="SoftwareNameDataGridColumn" TargetType="{x:Type DataGridCell}"&gt; 等声明第一个样式,并在后面的代码中使用与 FindResource 相同的技巧(名称将是 SoftwareNameDataGridColumn)
    • 编辑: 我错过了在我的尝试中我也将 x:Key 添加到第一个样式中......现在一切正常!再次感谢!!!
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-04
    • 1970-01-01
    相关资源
    最近更新 更多