Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my xaml:
XML
<!-- I used this to turn OFF the default cell selection color -->
<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border x:Name="border" removed="Transparent"
                               BorderBrush="Transparent" 
                               BorderThickness="1" 
                               SnapsToDevicePixels="True">
                    <ContentPresenter Name="CellContent" SnapsToDevicePixels="True" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


XML
<!-- Here are my row selection styles -->
<Style TargetType="DataGridRow" x:Key="Project">
    <Style.Triggers >
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Green" />
            <Setter Property="BorderBrush" Value="Green" />
        </Trigger>
    </Style.Triggers>
</Style>

<Style TargetType="DataGridRow" x:Key="Revision" BasedOn="{StaticResource Project}">
    <Style.Triggers >
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red" />
            <Setter Property="BorderBrush" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>


C#
//Here is the code that selects a row style based on data found in the row
private void chPrj_Checked(object sender, RoutedEventArgs e)
{
     var rws = (from item in Rows where item.projects.Contains((sender as CheckBox).Content.ToString()) select item);

    grdFileList.SelectionMode = DataGridSelectionMode.Extended;
    for (int i = 0; i < grdFileList.Items.Count; i++)
    {
       DataGridRow r = (DataGridRow) grdFileList.ItemContainerGenerator.ContainerFromIndex(i);
       SubstationDataRow gridRow = (SubstationDataRow)grdFileList.Items[i];
       if (gridRow.projects.Any(x => x == (sender as CheckBox).Content.ToString()))
       {
           grdFileList.SelectedItems.Add(grdFileList.Items[i]);
           r.Style = (Style)this.Resources["Project"];
           r.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
           updateRevListBox();
       }
    }
 }


The problem is:
I can get all the correct rows selected but, only the last row has the correct selection color. The other rows are selected but, they don't have the red or green background color as specified in the styles. Instead, they have the normal background color but, all the text in the row is selected. (That's how i can tell that all the correct rows are selected).

If I remove the styles, all the rows are correctly highlighted with the default selection color.
How can i get all of the selected rows to retain their custom selection color (red or green)?
I'm fairly new to WPF so, forgive for my ignorance. Any help would be greatly appreciated.
Posted
Updated 24-Jan-14 6:39am
v2
Comments
Varsha Ramnani 24-Jan-14 1:33am    
Why write code to do this & for every checkbox Checked why are you looping through all the rows and apply style again?

you can create a boolean property for ProjectSelected bind it to the checkbox and in trigger you can use that property to set the background, no need to write the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900