Files
Andy (Steve) De George e9fcf43083 Merge pull request #1135 from dotnet/adegeo-links
Correct .NET Framework links that pointed to old .NET 5 URLs
2021-08-11 12:04:34 -07:00

2.1 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
How to animate in a style 03/30/2017
animation [WPF], properties [WPF], within styles
styles [WPF], animating properties within
6a791f3d-6b1f-4972-a2f9-35880bcfd954

How to animate in a style

This example shows how to animate properties within a style. When animating within a style, only the framework element for which the style is defined can be targeted directly. To target a freezable object, you must "dot down" from a property of the styled element.

In the following example, several animations are defined within a style and applied to a xref:System.Windows.Controls.Button. When the user moves the mouse over the button, it fades from opaque to partially translucent and back again, repeatedly. When the user moves the mouse off the button, it becomes completely opaque. When the button is clicked, its background color changes from orange to white and back again. Because the xref:System.Windows.Media.SolidColorBrush used to paint the button can't be targeted directly, it is accessed by dotting down from the button's xref:System.Windows.Controls.Control.Background%2A property.

Example

[!code-xamltimingbehaviors_snip#21]

Note that when animating within a style, it's possible to target objects that don't exist. For example, suppose your style uses a xref:System.Windows.Media.SolidColorBrush to set a Button's background property, but at some point the style is overridden and the button's background is set with a xref:System.Windows.Media.LinearGradientBrush. Trying to animate the xref:System.Windows.Media.SolidColorBrush won't throw an exception; the animation will simply fail silently.

For more information about storyboard targeting syntax, see the Storyboards Overview. For more information about animation, see the Animation Overview. For more information about styles, see the Styling and Templating.