Share via

UWP how to change foreground color when mouse over?

kw Lee 0 Reputation points
2026-03-05T07:53:20.88+00:00

Below is my code in App.xaml. I could change the DataGridRow background color while mouse over, but I couldn't change the foreground color while mouse over. What I could add in the PointerOver part?

<Style x:Key="BasicDataGridRowStyle" TargetType="controls:DataGridRow">
                <Setter Property="IsTabStop" Value="False"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="controls:DataGridRow">
                            <prim:DataGridFrozenGrid x:Name="RowRoot">
                                <prim:DataGridFrozenGrid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </prim:DataGridFrozenGrid.ColumnDefinitions>
                                <prim:DataGridFrozenGrid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </prim:DataGridFrozenGrid.RowDefinitions>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="NormalAlternatingRow"/>
                                        <VisualState x:Name="PointerOver">
                                           
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Fill">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource RowHoverColor}" />
                                                </ObjectAnimationUsingKeyFrames>
                                                
                                                <DoubleAnimation Duration="0:0:.2" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" To="1"/>

                                            </Storyboard>
                                        </VisualState>
                                        	.
						.
						.
                                        
                                    </VisualStateGroup>
                                    
                                </VisualStateManager.VisualStateGroups>
                                <Rectangle x:Name="BackgroundRectangle" Grid.ColumnSpan="2" Fill="Transparent"/>
                                <Rectangle x:Name="InvalidVisualElement" Grid.ColumnSpan="2" Fill="{StaticResource SelectedRowColor}" Opacity="0"/>
                                <prim:DataGridRowHeader x:Name="RowHeader" prim:DataGridFrozenGrid.IsFrozen="True" Grid.RowSpan="3"/>
                                <prim:DataGridCellsPresenter x:Name="CellsPresenter" AutomationProperties.AccessibilityView="Raw" Grid.Column="1" prim:DataGridFrozenGrid.IsFrozen="True" MinHeight="32"/>
                                <prim:DataGridDetailsPresenter x:Name="DetailsPresenter" AutomationProperties.AccessibilityView="Raw" Grid.Column="1" Grid.Row="1"/>
                                <Rectangle x:Name="BottomGridLine" Grid.Column="1" HorizontalAlignment="Stretch" Height="1" Grid.Row="2"/>
                            </prim:DataGridFrozenGrid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

Developer technologies | Universal Windows Platform (UWP)
{count} votes

3 answers

Sort by: Most helpful
  1. Danny Nguyen (WICLOUD CORPORATION) 6,455 Reputation points Microsoft External Staff Moderator
    2026-03-05T09:59:09.74+00:00

    Hi @kw Lee ,

    You can’t animate CellsPresenter’s (Control.Foreground).(SolidColorBrush.Color) because in the UWP DataGrid template the prim:DataGridCellsPresenter is not a Control (so it doesn’t have a Foreground property). That’s why you get a runtime/template error on pointer-over. That’s why you get a runtime/template error on pointer-over..

    Instead, change the cell content’s foreground via a DataGridCell style.

    Foreground is applied on the cell / text element, not on the CellsPresenter. So set it on DataGridCell (or TextBlock) and use the cell’s own PointerOver visual state.

    Example:

    
    <!-- Foreground used on hover -->
    
    <SolidColorBrush x:Key="RowHoverForegroundBrush" Color="Red"/>
    
    <Style x:Key="BasicDataGridCellStyle" TargetType="controls:DataGridCell">
    
        <!-- default foreground -->
    
        <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
    
        <Setter Property="Template">
    
            <Setter.Value>
    
                <ControlTemplate TargetType="controls:DataGridCell">
    
                    <Grid x:Name="Root">
    
                        <ContentPresenter x:Name="ContentPresenter"
    
                                          Content="{TemplateBinding Content}"
    
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
    
                                          Foreground="{TemplateBinding Foreground}"
    
                                          Margin="{TemplateBinding Padding}"/>
    
                        <VisualStateManager.VisualStateGroups>
    
                            <VisualStateGroup x:Name="CommonStates">
    
                                <VisualState x:Name="Normal"/>
    
                                <VisualState x:Name="PointerOver">
    
                                    <Storyboard>
    
                                        <!-- This works because DataGridCell IS a Control -->
    
                                        <ObjectAnimationUsingKeyFrames
    
                                            Storyboard.TargetName="Root"
    
                                            Storyboard.TargetProperty="(Control.Foreground)">
    
                                            <DiscreteObjectKeyFrame KeyTime="0"
    
                                                Value="{StaticResource RowHoverForegroundBrush}"/>
    
                                        </ObjectAnimationUsingKeyFrames>
    
                                    </Storyboard>
    
                                </VisualState>
    
                            </VisualStateGroup>
    
                        </VisualStateManager.VisualStateGroups>
    
                    </Grid>
    
                </ControlTemplate>
    
            </Setter.Value>
    
        </Setter>
    
    </Style>
    
    

    Then apply it:

    
    <controls:DataGrid CellStyle="{StaticResource BasicDataGridCellStyle}" />
    
    

    If you want it to follow the row hover:

    You can still keep your DataGridRow hover background as-is, but foreground is best controlled at the cell level. If you want “row hover changes all cells”, the clean way is to also add triggers/visual states so the cell reacts when its parent row is pointer-over (usually done via Binding to an ancestor’s state or by using the built-in DataGrid default styles as a reference).


    If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance provide feedback. Thank you. 


  2. kw Lee 0 Reputation points
    2026-03-05T08:01:12.94+00:00

    Adding the code below shows error when mouse over the Data grid. How can I modify the code?

    <!-- New foreground animation -->
            <ColorAnimation
                Storyboard.TargetName="CellsPresenter"
                Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                To="Red"             Duration="0:0:0.2" />
    
    
    0 comments No comments

  3. Q&A Assist
    2026-03-05T07:53:33.6033333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    In the PointerOver visual state, add a storyboard animation that targets the foreground (typically the Foreground of the cells or a brush used for text) in the same way you target BackgroundRectangle.Fill.

    For UWP XAML, this is done by animating a brush property via a Storyboard in the VisualState.Storyboard. The pattern is the same as in the examples where a Grid background is animated between states using Storyboard.TargetName and Storyboard.TargetProperty.

    Assuming the text in the row uses a named element (for example, CellsPresenter) and its Foreground is a SolidColorBrush, add a ColorAnimation in the PointerOver state storyboard, and another in the Normal state to restore it. Conceptually:

    <VisualState x:Name="Normal">
        <Storyboard>
            <!-- Restore foreground color -->
            <ColorAnimation
                Storyboard.TargetName="CellsPresenter"
                Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                To="YourNormalColor"
                Duration="0:0:0.2" />
        </Storyboard>
    </VisualState>
    
    <VisualState x:Name="PointerOver">
        <Storyboard>
            <!-- Existing background animation -->
            <ObjectAnimationUsingKeyFrames
                Storyboard.TargetName="BackgroundRectangle"
                Storyboard.TargetProperty="Fill">
                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource RowHoverColor}" />
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimation Duration="0:0:.2"
                             Storyboard.TargetName="BackgroundRectangle"
                             Storyboard.TargetProperty="Opacity" To="1"/>
    
            <!-- New foreground animation -->
            <ColorAnimation
                Storyboard.TargetName="CellsPresenter"
                Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)"
                To="YourHoverColor"
                Duration="0:0:0.2" />
        </Storyboard>
    </VisualState>
    

    Key points:

    • Use VisualState.Storyboard to define state-specific property changes.
    • Target a named element in the template (Storyboard.TargetName) that actually owns the Foreground used by the row’s text.
    • Use Storyboard.TargetProperty to point to the brush color property, e.g. (Control.Foreground).(SolidColorBrush.Color) or the appropriate property path for your element.

    This follows the same mechanism described for animating template elements between visual states using storyboards.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.