Highlight specific rows

Sep 2, 2008 at 10:39 AM
Hi all,

Need help to set specific row background color that matches certain conditions in the columns.

Let say I have a column that has a bool value. Now I want to highlight all rows where the column
values are false.

Thanks!
Coordinator
Sep 2, 2008 at 11:47 PM
When you say a column that has a bool value, do you mean the bool value as in the content it carries?  If so, you can do something like this:

<dg:DataGridCheckBoxColumn DataFieldBinding="{Binding Path=LikesCake}"  Header="LikesCakeColumn"/>

But for all rows you can set it like so:

<Style x:Key="defaultRowStyle" TargetType="{x:Type dg:DataGridRow}">
        <Setter Property="Background" Value="{Binding LikesCake, Converter={StaticResource BackgroundConverter}}"/>
</Style>

Then when the bool value changes for the CheckBoxColumn the row background for any row will change according to the BackgroundConverter.
Sep 3, 2008 at 8:31 AM
Thanks for the info.

Basically what I want is to highlight all rows that is true.

In you example all rows that is cakes.

Thanks again!
Sep 9, 2008 at 10:24 PM
Hi,
I have an issue with using this scenario while using AlternatingRowBackground.  If a row meets my criteria and I change the background color, the AlternatingRowBackground seems to take over if I scroll the row out of view and back into view.  May have something to do with the VirtualizingStackPanel?
Coordinator
Sep 10, 2008 at 2:06 PM
Can you show me how you are changing the background based on your criteria.
Sep 11, 2008 at 5:22 AM
Edited Sep 11, 2008 at 5:23 AM
Well, I was originally using a hybrid approach.  A combination of AlternatingRowBackGround and in my codebehind I had a class that inherited StyleSelector.  I've since abandoned that approach and opted for something less complicated.  Now I set the AlternationCount=2 on my DataGrid then use a Style with a targetType of DataGridRow since I want all of the rows to behave the same, I just want the Background and Foreground color to be different depending on my criteria.  I pass my values to my IMultiValueConverters.

<Style TargetType="{x:Type dg:DataGridRow}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="FontWeight" Value="Bold"/>
                </Trigger>
            </Style.Triggers>
            <Setter Property="Background">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource RowBackgroundColorConverter}" Mode="Default" ConverterParameter="">
                                <Binding Path="Bool1"/>
                                <Binding Path="Bool2"/>                               
                                <Binding RelativeSource="{RelativeSource Self}" Path="(ItemsControl.AlternationIndex)"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground">
                        <Setter.Value>
                            <MultiBinding Converter="{StaticResource RowForegroundColorConverter}" Mode="Default" ConverterParameter="">
                                <Binding Path="Bool1"/>
                                <Binding Path="Bool2"/>
                                <Binding RelativeSource="{RelativeSource Self}" Path="(ItemsControl.AlternationIndex)"/>
                            </MultiBinding>
                        </Setter.Value>
                    </Setter>
        </Style>