Home > Silverlight > Silverlight datagrid change foreground color on VisualStateManager(mouseover, selected)

Silverlight datagrid change foreground color on VisualStateManager(mouseover, selected)

Here is how you could change font properties using the VisualStateManager in a Silverlight datagrid.

The main problem is that when trying to edit a copy of the grid style the row content is presented as a DataGridCellPresenter, which doesn’t have any font properties. So what you can do is to wrap the DataGridCellPresenter into a contentControl so this way you can modify its properties and the  DataGridCellPresenter would inherit them.

Here is an example:

<ContentControl x:Name=”contentControl” Foreground=”#FF000000″>
  <Primitives:DataGridCellsPresenter x:Name=”CellsPresenter” Grid.Column=”1″
                                                                            Primitives
:DataGridFrozenGrid.IsFrozen=”True”/>
</
ContentControl>
<vsm:VisualState x:Name=”MouseOver”>
  <Storyboard>
   
<ColorAnimationUsingKeyFrames BeginTime=”00:00:00″ Duration=”00:00:00.0010000″
                                                                           Storyboard.TargetName
=”contentControl”
                                                                          
Storyboard.TargetProperty=”(Control.Foreground).(SolidColorBrush.Color)”>
     
<SplineColorKeyFrame KeyTime=”00:00:00″ Value=”#FFE13710E”/>
   
</ColorAnimationUsingKeyFrames>
 
</Storyboard>
</vsm:VisualState>
<vsm:VisualState  x:Name=”Normal Selected”>
 
<Storyboard>
   
<DoubleAnimationUsingKeyFrames Storyboard.TargetName=”BackgroundRectangle”
                                                                               Storyboard.TargetProperty
=”Opacity”>
     
<SplineDoubleKeyFrame KeyTime=”0″ Value=”1″/>
   
</DoubleAnimationUsingKeyFrames>
   
<ColorAnimationUsingKeyFrames BeginTime=”00:00:00″ Duration=”00:00:00.0010000″
                                                                            Storyboard.TargetName
=”contentControl”
                                                                            Storyboard.TargetProperty
=”(Control.Foreground).(SolidColorBrush.Color)”>
     
<SplineColorKeyFrame KeyTime=”00:00:00″ Value=”#FFE12222″/>
   
</ColorAnimationUsingKeyFrames>
 
</Storyboard>
</vsm:VisualState>
 
Grid
  1. Jonathan
    July 31, 2009 at 1:05 pm | #1

    So on what would you set the style? A custom DataTemplate row?

    • alejandrobog
      September 23, 2009 at 4:01 am | #2

      You set the style to the ContentControl, through an animation. Like in the example above.

      <Storyboard>
      <ColorAnimationUsingKeyFrames BeginTime=”00:00:00″ Duration=”00:00:00.0010000″
      Storyboard.TargetName=”contentControl”
      Storyboard.TargetProperty=”(Control.Foreground).(SolidColorBrush.Color)”>
      <SplineColorKeyFrame KeyTime=”00:00:00″ Value=”#FFE13710E”/>
      </ColorAnimationUsingKeyFrames>
      </Storyboard>

  2. Ben Lowe
    October 14, 2009 at 9:25 am | #3

    Make sure you set the following properties on the ContentControl:

    dataprimitives:DataGridFrozenGrid.IsFrozen=”True”
    IsTabStop=”False”

    Without these you will get strange tabbing and scrolling issues.

    Ben

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.