Unable to set Storyboard Target in code
Here is a sample with an identical storyboard in code behind and XAML. The XAML storyboard works fine, but in the code behind with Noesis I get "Cannot Begin Storyboard, Target not found"
The asserts all pass. And the object is recognized/valid/instantiated in the code behind.
Code: Select all
<StackPanel>
<Button x:Name="XAMLButton" Width="50" Height="50" Background="DarkSeaGreen" Content="XAML">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard Storyboard.Target="{Binding ElementName=XAMLButton}" Storyboard.TargetProperty="Width">
<DoubleAnimation To="200" Duration="0:0:2"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Button x:Name="CodeButton" Width="50" Height="50" Click="CodeButton_Click" Background="DarkGreen" Content="Code"/>
</StackPanel>
Code: Select all
private void CodeButton_Click(object sender, RoutedEventArgs e)
{
DoubleAnimation d = new DoubleAnimation();
d.Duration = new Duration(TimeSpan.FromSeconds(2));
d.To = 200;
Storyboard sb = new Storyboard();
sb.Children.Add(d);
Storyboard.SetTarget(sb, CodeButton);
Storyboard.SetTargetProperty(sb, new PropertyPath("Width"));
Storyboard.SetTargetName(sb, "CodeButton");
Debug.Assert(CodeButton.Name == "CodeButton");
Debug.Assert(Storyboard.GetTarget(sb) == CodeButton);
Debug.Assert(Storyboard.GetTargetProperty(sb).Path == "Width");
Debug.Assert(Storyboard.GetTargetName(sb) == "CodeButton");
sb.Begin();
}
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Unable to set Storyboard Target in code
I can't check it right now but I think this happens because the Storyboard is created in code and launched with no parameters Begin, we probably have something wrong in our code in that case. Could you please try calling instead
Code: Select all
sb.Begin(CodeButton);
Re: Unable to set Storyboard Target in code
Yip, that's the issue. Works fine when specifying the object as a parameter.I can't check it right now but I think this happens because the Storyboard is created in code and launched with no parameters Begin, we probably have something wrong in our code in that case. Could you please try calling insteadCode: Select allsb.Begin(CodeButton);
Re: Unable to set Storyboard Target in code
There's another possibly related issue here as well. It seems that Storyboards are not inheriting their FillBehavior. Here is a XAMLToy example:
The FillBehavior of the parent is ignored by the DoubleAnimation which will behave as if its FillBehavior was HoldEnd.
Code: Select all
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Rectangle Width="50" Height="50" Fill="DarkSeaGreen">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Grid.Loaded">
<BeginStoryboard>
<Storyboard FillBehavior="Stop" Storyboard.TargetProperty="Width">
<DoubleAnimation To="200" Duration="0:0:2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Grid>
Re: Unable to set Storyboard Target in code
Hi, do you plan to get this/these patched up for the next release? I'm always conflicted about whether to report these sort of issues on the forum or in the bugtracker. Thanks.
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Unable to set Storyboard Target in code
For all questions that turn into bugs/features it is preferable to create a ticket in the bugtracker so we can include them in the roadmap.
Could you please report both as separate issues (Storyboard.Begin() and FillBehavior inheritance)?
Could you please report both as separate issues (Storyboard.Begin() and FillBehavior inheritance)?
-
sfernandez
Site Admin
- Posts: 2984
- Joined:
Re: Unable to set Storyboard Target in code
Thanks for the reports.
Who is online
Users browsing this forum: No registered users and 90 guests