A Microsoft platform for building and publishing apps for Windows devices.
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.