Setting a Selected Color does not work when Alternate row color is set

Sep 22, 2008 at 10:05 PM

 

<Style x:Key="dataGridRowStyle"

 

 

TargetType="dg:DataGridRow" >

 

 

<Style.Triggers>

 

 

<Trigger Property="IsSelected" Value="true">

 

 

<Setter Property="Background" Value="LightBlue" />

 

 

</Trigger>

 

 

</Style.Triggers>

 

 

</Style>

This simple xaml will not work to set the Seleted Row Background color when we have 

 

AlternatingRowBackground

="LightGray" AlternationCount="2"

Are we doing something wrong?

 

Sep 22, 2008 at 11:21 PM
Try changing the TargetType to "dg:DataGridCell" and set the CellStyle property of the DataGrid.
John
Sep 23, 2008 at 11:03 AM
Edited Sep 23, 2008 at 11:11 AM
Yeah, no dice.. This just sets the Background of the cell not the background of the row..

It seems that if we set the RowBackground and AlternatingRowBackground color properties of the grid that the xaml cannot override those color settings. However, if we do not set those properties at the Grid level and use a Converter (see below) to initialize the row and alternate row colors the xaml works like a champ.

XAML - Simply set the Cell and Row style of the grid to these styles and do not specify RowBackground or Alternating background colors on the Grid.

 

<Style x:Key="dataGridCellStyle"

 

 

TargetType="{x:Type dg:DataGridCell}">

 

 

<Setter Property="HorizontalAlignment"

 

 

Value="Center" />

 

 

<Setter Property="VerticalAlignment"

 

 

Value="Center" />

 

 

<Setter Property="Margin"

 

 

Value="4,2,0,2" />

 

 

<Style.Triggers>

 

 

<Trigger Property="IsSelected" Value="True">

 

 

<Setter Property="Background"

 

 

Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type dg:DataGridRow}}, Path=Background}" />

 

 

<Setter Property="BorderBrush"

 

 

Value="Transparent" />

 

 

<Setter Property="Foreground"

 

 

Value="Black" />

 

 

</Trigger>

 

 

</Style.Triggers>

 

 

</Style>

 

 

<Style x:Key="dataGridRowStyle"

 

 

TargetType="dg:DataGridRow" >

 

 

<Setter Property="Background">

 

 

<Setter.Value>

 

 

<MultiBinding Converter="{StaticResource dataGridRowBackgroundColorConverter}">

 

 

<Binding RelativeSource="{RelativeSource Self}" Path="(ItemsControl.AlternationIndex)"/>

 

 

</MultiBinding>

 

 

</Setter.Value>

 

 

</Setter>

 

 

<Style.Triggers>

 

 

<Trigger Property="IsSelected" Value="true">

 

 

<Setter Property="Background" Value="LightBlue" />

 

 

</Trigger>

 

 

</Style.Triggers>

 

 

</Style>

 



CODE - We used a MultiConverter but you could just use ye ole IValueConverter in this example since you only pass the alternating index value

class

DataGridRowBackgroundColorConverter : IMultiValueConverter

 

{

#region

IMultiValueConverter Members

 

 

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)

 

{

 

Brush result = Brushes.White;

 

 

//bool isSelected;

 

 

int alterationIndex;

 

 

//if (!Boolean.TryParse(values[0].ToString(), out isSelected)) return result;

 

 

if (!int.TryParse(values[0].ToString(), out alterationIndex)) return result;

 

 

if(alterationIndex == 1)

 

{

result =

Brushes.LightGray;

 

}

 

return result;

 

}

 

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)

 

{

 

throw new NotImplementedException();

 

}

#endregion

}

Nov 10, 2008 at 9:55 PM
Hi,

I would like to also set a mouse over style, that overrides the value "AlternatingRowBackground" color - Is the solution above the recommended approach?  Here is some of my xaml....

<tk:DataGrid x:Name="Grid" SelectionUnit="FullRow"
AlternatingRowBackground="#f9f9f9"

.....
    <Style x:Key="DataGridRowStyle" TargetType="{x:Type tk:DataGridRow}">
                <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="#FFFFFFE0" />
            </Trigger>


Thanks!
Jon
Coordinator
Nov 11, 2008 at 1:18 AM
Edited Nov 11, 2008 at 1:19 AM

I think the problem is to do with the Precedence of properties. AlternatingRowBackground / RowBackground set as a local values have more precedence than Background set as IsSelected / IsMouseOver trigger in style. Hence it never gets picked up. If you set both AlternatingRowBackground and RowBackground in style of DataGrid and set RowBackground based on IsSelected/IsMouseOver as a trigger in style, it should get picked up. Here is what I mean....

       <Style x:Key="defaultRowStyle" TargetType="{x:Type dg:DataGridRow}">

            <Style.Triggers>

                <Trigger Property="IsMouseOver" Value="true">

                    <Setter Property="Background" Value="White"/>

                </Trigger>

            </Style.Triggers>

        </Style>

 

        <Style TargetType="{x:Type dg:DataGrid}">

            <Setter Property="RowBackground" Value="Yellow"></Setter>

            <Setter Property="AlternatingRowBackground" Value="Cyan"></Setter>

        </Style>

        <dg:DataGrid> //Without   actually specify RowBackground or AlternatingRowBackground

        …….

        </dg:DataGrid>

Nov 11, 2008 at 1:52 PM
Yes, Yes, Yes! Thank you, that makes complete sense! (and works of course!)