根据附加属性更改DataGrid列的背景颜色

时间:2018-02-17 23:01:04

标签: wpf datagrid styles attached-properties

我正在尝试设置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附加属性是错误的,因为它永远不会触发颜色的变化,我想因为我有附加到列而不是 int upperLimit = 30; //Set your upper limit here System.out.println(2); for(int i = 2; i < upperLimit; i++) for(int j = 2; j < i; j++) if(i % j == 0 && i != 2) break; else if(j == i - 1) System.out.println(i); ,但我无法弄清楚如何使其工作,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您正在设置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>