Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StoryBoard - Set Target Name from code-behind

Tags:

c#

wpf

storyboard

I have StoryBoard in resources

  <Window.Resources>
    <Storyboard x:Key="Fading" Storyboard.TargetName="NotifyWindow" Storyboard.TargetProperty="Opacity" >
        <DoubleAnimation From="1" To="0" Duration="0:0:1">
        </DoubleAnimation>
    </Storyboard>
  </Window.Resources>

And on WindowClosing I have next code

private void NotifyWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    Storyboard fading = this.Resources["Fading"] as Storyboard;
    if (fading != null && !fadingCompleted)
    {
        fading.Completed += FadingStoryBoard_Completed;
        fading.Begin();
        e.Cancel = true;
    }
}

private void FadingStoryBoard_Completed(object sender, EventArgs e)
{
   fadingCompleted = true;
   Close();
   fadingCompleted = false;
}

And this works fine, But I want to move this storyboard to another assembly. So i need to specify StoryBoard.TargetName form code. How can I do that?

like image 805
Stecya Avatar asked Mar 14 '11 15:03

Stecya


2 Answers

Attached properties can be set from code via static helper methods named: "Set" + PropertyName

See C# example here:

Storyboard.SetTargetName(yourAnimation, "NotifyWindow"); 
like image 64
Snowbear Avatar answered Oct 19 '22 04:10

Snowbear


The standard way to set dependency properties to dependency objects is the same for attached properties :

dependencyObjectInstance.SetValue(SampleClass.PropertyName + "Property", value);

In your example :

fading.SetValue(Storyboard.TargetNameProperty, "NotifyWindow");
like image 29
Necriis Avatar answered Oct 19 '22 03:10

Necriis