如何在WPF中按行绑定DataGrid.HighlightBrush

时间:2012-08-29 20:33:58

标签: wpf binding

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

问题是,当选择行时,它们都会变成均匀的颜色,再也无法确定状态。我想以类似的方式绑定高亮颜色:

            <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;
        }
    }

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

1 个答案:

答案 0 :(得分: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 );