Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Selecting the Target of an Animation
    primarykey
    data
    text
    <p>I am trying to create a simple (I think) animation effect based on a property change in my ViewModel. I would like the target to be a specific textblock in the control template of a custom control, which inherits from Window.</p> <p>From the article examples I've seen, a DataTrigger is the easiest way to accomplish this. It appears that Window.Triggers doesn't support DataTriggers, which led me to try to apply the trigger in the style. The problem I am currently having is that I can't seem to target the TextBlock (or any other child control)--what happens is which the code below is that the animation is applied to the background of the whole window.</p> <p>If I leave off StoryBoard.Target completely, the effect is exactly the same.</p> <p>Is this the right approach with the wrong syntax, or is there an easier way to accomplish this?</p> <pre><code>&lt;Style x:Key="MyWindowStyle" TargetType="{x:Type Window}"&gt; &lt;Setter Property="Template" Value="{StaticResource MyWindowTemplate}"/&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding ChangeOccurred}" Value="True"&gt; &lt;DataTrigger.EnterActions&gt; &lt;BeginStoryboard&gt; &lt;Storyboard BeginTime="00:00:00" Duration="0:0:2" Storyboard.Target="{Binding RelativeSource={RelativeSource AncestorType=TextBlock}}" Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)"&gt; &lt;ColorAnimation FillBehavior="Stop" From="Black" To="Red" Duration="0:0:0.5" AutoReverse="True"/&gt; &lt;/Storyboard&gt; &lt;/BeginStoryboard&gt; &lt;/DataTrigger.EnterActions&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p><strong>Update</strong></p> <p>Should have also mentioned that I tried to name the TextBlock and reference it via StoryBoard.TargetName (as Timores suggested), and got the error "TargetName property cannot be set on a Style Setter."</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload