StoryBoard - Set Target Name from code-behind
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 sende开发者_StackOverflow中文版r, 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?
Attached properties can be set from code via static helper methods named: "Set" + PropertyName
See C# example here:
Storyboard.SetTargetName(yourAnimation, "NotifyWindow");
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");
精彩评论