1

我有一个数据网格,行的颜色不同,具体取决于其中记录的状态(有效为白色,问题为金色,禁止为红色)。

The problem is when the row is selected they all turn a uniform color and it becomes impossible to determine the status anymore. 我想以与此类似的方式绑定突出显示颜色:

            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="{Binding Member, Converter={StaticResource MemberBackgroundConverter}}" />
                    <Setter Property="HighlightBrushKey" Value="{Binding Member, Converter={StaticResource MemberHighlightConverter}}" />                        
                </Style>
            </DataGrid.RowStyle>

上面的第一个 Setter 有效。有没有办法让第二个工作?有什么方法可以设置每行的 HighlightBrush 吗?

编辑:以下是我目前正在工作的内容。我并不是说这是最好的方法,只是这种方法有效。

XAML:

<DataGrid.Resources>
    <SolidColorBrush x:Key="SelectionBackgroundColorKey" />

    <LinearGradientBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="0.0" />
        <GradientStop Color="White" Offset="0.3" />
        <GradientStop Color="{Binding Source={StaticResource SelectionBackgroundColorKey}, Path=Color}" Offset="1.0" />
    </LinearGradientBrush>

    <SolidColorBrush x:Key="SelectionTextColorKey" Color="Black" />
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{Binding Source={StaticResource SelectionTextColorKey}, Path=Color}" />
</DataGrid.Resources>

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="Background" Value="{Binding BindsDirectlyToSource=True, Converter={StaticResource ReservationBackgroundConverter}}" />
        <Setter Property="Foreground" Value="{Binding IsNew, Converter={StaticResource IsNewForegroundConverter}}" />
    </Style>
</DataGrid.RowStyle>

代码:

    private void DataGridReservationsSelectionChanged(object argSender, SelectionChangedEventArgs argEvtArgs)
    {
        Reservation LocalReservation;

        ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.SlateGray;
        ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;

        LocalReservation = dataGridReservations.SelectedItem as Reservation;

        if (LocalReservation == null)
        {
            return;
        }

        if(LocalReservation.IsArrived)
        {
            ((SolidColorBrush)dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.ForestGreen;
            ((SolidColorBrush)dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
            return;
        }
        //Is this Reservation a Problem?
        if (LocalReservation.Member.IsProblem)
        {
            ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Goldenrod;
            ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
            return;
        }

        //Is this Reservation Banned?
        if (LocalReservation.Member.IsBanned)
        {
            ((SolidColorBrush) dataGridReservations.Resources["SelectionBackgroundColorKey"]).Color = Colors.Firebrick;
            ((SolidColorBrush) dataGridReservations.Resources["SelectionTextColorKey"]).Color = Colors.Black;
            return;
        }
    }

这种方法允许我为每个独立行设置未选择的颜色,并为每个独立行设置选定的颜色。

4

1 回答 1

0

如果使用 DataGridTextColumn,您可以指定 ElementStyle:

<DataGrid.Resources>
    <Style x:Key="MemberCellStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="{Binding Member, Converter={StaticResource MemberBackgroundConverter}}" />
    </Style>
</DataGrid.Resources>

...

 <DataGridTextColumn ... ElementStyle="{StaticResource MemberCellStyle}">

无论是否选择了行,背景颜色都将遵守绑定。

更新:

您可以使用 MultiDataTrigger 并创建 2 个条件(1 个用于您的状态,1 个用于该行是否被选中)。这是一个在代码中创建样式的示例(针对选定和未选定条件),但如果需要,它应该很容易转换为 XAML:

Style cellStyle = new Style( typeof( DataGridCell ) );
MultiDataTrigger triggerNotSelected = new MultiDataTrigger();
Condition dataCondition = new Condition( new Binding( "[STATUS]", [STATUSVALUE] );
Condition rowNotSelectedCondition = new Condition();
rowNotSelectedCondition.Binding = new Binding
     {
          Path = new PropertyPath( DataGridRow.IsSelectedProperty ),
          RelativeSource = new RelativeSource( RelativeSourceMode.FindAncestor, typeof( DataGridRow ), 1 )
     };
rowNotSelectedCondition.Value = false;

triggerNotSelected.Conditions.Add( dataCondition );
triggerNotSelected.Conditions.Add( rowNotSelectedCondition );
triggerNotSelected.Setters.Add( new Setter( Control.BackgroundProperty, Brushes.Gold ) );

MultiDataTrigger triggerSelected = new MultiDataTrigger();
Condition rowSelectedCondition = new Condition();
rowSelectedCondition.Binding = new Binding
    {
         Path = new PropertyPath( DataGridRow.IsSelectedProperty ),
         RelativeSource = new RelativeSource( RelativeSourceMode.FindAncestor, typeof( DataGridRow ), 1 )
    };
rowSelectedCondition.Value = true;

triggerSelected.Conditions.Add( dataCondition );
triggerSelected.Conditions.Add( rowSelectedCondition );
triggerSelected.Setters.Add( new Setter( Control.BackgroundProperty, Brushes.Khaki ) );

cellStyle.Triggers.Add( triggerNotSelected );
cellStyle.Triggers.Add( triggerSelected );
于 2012-08-31T20:47:57.433 回答