concept target in category silverlight

appears as: target, target
Silverlight 4 in Action: Silverlight 4, MVVM, and WCF RIA Services

This is an excerpt from Manning's book Silverlight 4 in Action: Silverlight 4, MVVM, and WCF RIA Services.

Dependency properties are part of the property system introduced with WPF and used in Silverlight. In markup and in consuming code, they’re indistinguishable from standard .NET CLR properties, except that they can be data bound, serve as the target of an animation, or set by a style.

Figure 11.1. A conceptual view of data binding. The source owns the data; the target operates on (displays, edits, and so forth) the data.

This example shows how the EventTrigger has been added to a Canvas. Significantly, this doesn’t mean that the target of the Storyboard will be the Canvas. Instead, as discussed earlier, the target of the Storyboard is set within the Storyboard itself. To set the target of the Storyboard, you first define the Storyboard.

22.3.2. Hitting the target

As mentioned earlier, the Storyboard exposes two attached properties that can be used to set the target of an animation. The first is TargetName, and the second is TargetProperty. These two property values are codependent and both are required to create an animation. Without these values, your animations won’t know what to animate. If you define these two values within a Storyboard, you can share their values across the child Timeline elements.

As shown in the previous listing, the Storyboard uses the TargetName attached property to specify the target of the animation. Each of the child animations uses the same target element. If one of these animations needs to use a different element, you can trump this value by providing a different TargetName value, using the approach shown in listing 22.4.

Listing 22.4. Animation overriding target of its parent Storyboard
<Storyboard x:Name="myStoryboard" Storyboard.TargetName="myRectangle"> <ColorAnimation x:Name="myColorAnimation" Duration="00:00:03" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" From="Green" To="Blue" /> <DoubleAnimation x:Name="myDoubleAnimation" Duration="00:00:03" Storyboard.TargetName="myRectangle2" Storyboard.TargetProperty="Opacity" From="0" To="1" /> <DoubleAnimation x:Name="myDoubleAnimation2" Duration="00:00:05" Storyboard.TargetProperty="Width" To="180" /> </Storyboard> ... <Rectangle x:Name="myRectangle" Width="180" Height="120" Fill="Green" /> <Rectangle x:Name="myRectangle2" Width="90" Height="30" Fill="Pink" />

This listing defines the primary target of the Storyboard as myRectangle. This target is used by the myColorAnimation and myDoubleAnimation2 animations. myDoubleAnimation uses myRectangle2 as the target instead of myRectangle. This is accomplished by overriding the TargetName value set in the storyboard itself. Note that each of the animations in this listing targets a separate property.