Antoine Blanchet
Antoine Blanchet

Reputation: 325

How apply a storyboard to a Label programmatically?

I've a little problem, I'd search google and my Wpf's books but I don't found any answer :(

I have created a little storyboard :

<Storyboard x:Key="whiteAnim" Duration="1">
        <ColorAnimation By="Black" To="White" Storyboard.TargetProperty="Background" x:Name="step1"/>
        <ColorAnimation By="White" To="Black" Storyboard.TargetProperty="Background" x:Name="step2"/>
</Storyboard>

This animation will change background color from black to white, and from white to black. I want to "apply" this storyboard to a Label :

Label label = new Label();
label.Content = "My label";

I'm looking for a method like "label.StartStoryboard(--myStoryboard--), do you have any ideas ?

Thank you :)

Upvotes: 1

Views: 600

Answers (1)

Jehof
Jehof

Reputation: 35554

It should work with

public void StartStoryboard() {
  whiteAnim.Target = label;
  whiteAnim.Begin();
}

or

public void StartStoryboard() {
  Storyboard.SetTarget(whiteAnim, label);
  whiteAnim.Begin();
}

Upvotes: 2

Related Questions