Wpf 根据附加属性更改DataGrid列的背景颜色
我正在尝试设置一个Wpf 根据附加属性更改DataGrid列的背景颜色,wpf,datagrid,styles,attached-properties,Wpf,Datagrid,Styles,Attached Properties,我正在尝试设置一个DataGrid的样式,这样,如果一个列有一个附加属性,就可以指示它是否高亮显示。因此,Highlighted=true列中的单元格与Highlighted=false列中的单元格颜色不同 我的附加属性看起来像: public static class Highlighted { public static bool GetIsHighlighted(DependencyObject obj) { return (bool)obj.GetValu
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));
}
<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>
而DataGridCell
样式如下所示:
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));
}
<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>
但是在几个令人沮丧的小时后,我不知道如何让它工作,我的
样式.Trigger
对于附加属性是错误的,因为它从不触发颜色的变化,我猜是因为我将属性附加到列,而不是DataGridCell
,但我不知道如何让它工作,任何帮助都将不胜感激。您正在设置该列的IsHighlighted
附加属性。这与在单元格上设置它不同
您应该在最终由列创建的单元格上设置它。在纯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>