【问题标题】:Change background colour of a DataGrid column based on attached property根据附加属性更改 DataGrid 列的背景颜色
【发布时间】:2018-02-17 23:01:03
【问题描述】:

我正在尝试设置DataGrid 的样式,以便列是否具有附加属性以指示它是否突出显示。因此,Highlighted = true 列中的单元格与Highlighted = false 列中的单元格颜色不同。

我的附加属性如下所示:

public static class Highlighted
{
    public static bool GetIsHighlighted(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsHighlightedProperty);
    }

    public static void SetIsHighlighted(DependencyObject obj, bool value)
    {
        obj.SetValue(IsHighlightedProperty, value);
    }

    public static readonly DependencyProperty IsHighlightedProperty =
        DependencyProperty.RegisterAttached("IsHighlighted", typeof(bool), typeof(Highlighted), new UIPropertyMetadata(false));
}

DataGridCell 样式看起来像:

<Style x:Key="WeldOfficeDataGridCell" TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border BorderBrush="{x:Static SystemColors.ActiveBorderBrush}"
                        BorderThickness="0.5"
                        Background="FloralWhite" SnapsToDevicePixels="True">
                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" 
                                      Content="{TemplateBinding Content}" 
                                      ContentStringFormat="{TemplateBinding ContentStringFormat}" 
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                      VerticalAlignment="Center"
                                      Margin="15,5,5,5" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="attachedProperties:Highlighted.IsHighlighted" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="Red" />
                </Setter.Value>
            </Setter>
        </Trigger>

        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background">
                <Setter.Value>
                    <SolidColorBrush Color="{DynamicResource AccentColor2}" />
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

我在DataGrid 中设置属性,比如:

    <DataGrid Margin="27,17,0,0"
              Grid.Column="1"
              ItemsSource="{Binding FilterableBaseMaterials}" 
              AutoGenerateColumns="False" 
              SelectionMode="Single"
              HeadersVisibility="Column"
              CanUserAddRows="False"
              CanUserDeleteRows="False"
              CanUserResizeRows="False"
              Background="{x:Null}">

        <DataGrid.Resources>
            <helpers:BindingProxy x:Key="proxy" Data="{Binding}" />
        </DataGrid.Resources>

        <DataGrid.Columns>

            <DataGridTemplateColumn Header="Specification" IsReadOnly="True" attachedProperties:Highlighted.IsHighlighted="True">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock HorizontalAlignment="Left" Text="{Binding Specification}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

            ...

但经过几个令人沮丧的小时后,我无法弄清楚如何让它工作,我的附加属性的Style.Trigger 是错误的,因为它永远不会触发颜色变化,我想是因为我将属性附加到列上并且不是DataGridCell,但我不知道如何让它工作,任何帮助将不胜感激。

【问题讨论】:

    标签: wpf datagrid styles attached-properties


    【解决方案1】:

    您正在设置 IsHighlighted 附加属性。这与将其设置在 cell 上不同。

    您应该将它设置在最终由列创建的单元格上。在纯 XAML 中执行此操作的唯一方法是为列定义 CellStyle

    <DataGridTemplateColumn Header="Specification" IsReadOnly="True">
        <DataGridTemplateColumn.CellStyle>
            <Style TargetType="DataGridCell" BasedOn="{StaticResource WeldOfficeDataGridCell}">
                <Setter Property="attachedProperties:Highlighted.IsHighlighted" Value="True" />
            </Style>
        </DataGridTemplateColumn.CellStyle>
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock HorizontalAlignment="Left" Text="{Binding Specification}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 2020-01-24
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      • 2019-10-16
      • 2013-10-24
      相关资源
      最近更新 更多