1

I am trying to change the rowstyle for a datagrid to change the background color when a row is selected(not the row background but the color of the selection rectangle, which by default is a bit too shiny for my application). However the behaviour i'm seeing on applying to style to the grid is that all the rows are getting shrinked together like a folded venetian blind. This is the style that i have written:

<Style x:Key="DataGridRowStyle" TargetType="sdk:DataGridRow">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="sdk:DataGridRow">
                        <sdk:DataGrid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">                                       
                                    <VisualState x:Name="Normal Selected">
                                        <Storyboard>
                                            <ColorAnimation 
                                                Storyboard.TargetName="ButtonBrush" 
                                                Storyboard.TargetProperty="Color" 
                                                To="Red" />                                            
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </sdk:DataGrid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Can someone tell me what's going on here? tbh I havn't really understood how the visual states work and kind of just shooting in the dark.

Thanks

1 Answer 1

2

Checking the template for the DataGrid (check here) the background color (that blue) is hard coded in the template. When the row is selected all that happens is that a rectangle becomes visible (Opacity=1). Sadly have to say that you have to re-template the whole DataGridRow... in the provided link you'll find all you might need for this.

In the template look for some code like this: <Rectangle x:Name="BackgroundRectangle" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#FFBADDE9"/>

1
  • That link was very useful. I took the entire control template for the datagridrow and changed the parts related to Normal selected state and it seems to be working fine. Thank you.
    – user642770
    Jun 15, 2011 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.