Using a Storyboard animation on a programmatically-added control
Solution 1
I solved the problem using this as parameter in the begin method, try:
sb.Begin(this);
Because the name is registered in the window.
Solution 2
I agree, the namescopes are probably the wrong thing to use for this scenario. Much simpler and easier to use SetTarget rather than SetTargetName.
In case it helps anyone else, here's what I used to highlight a particular cell in a table with a highlight that decays to nothing. It's a little like the StackOverflow highlight when you add a new answer.
TableCell cell = table.RowGroups[0].Rows[row].Cells[col];
// The cell contains just one paragraph; it is the first block
Paragraph p = (Paragraph)cell.Blocks.FirstBlock;
// Animate the paragraph: fade the background from Yellow to White,
// once, through a span of 6 seconds.
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
p.Background = brush;
ColorAnimation ca1 = new ColorAnimation()
{
From = Colors.Yellow,
To = Colors.White,
Duration = new Duration(TimeSpan.FromSeconds(6.0)),
RepeatBehavior = new RepeatBehavior(1),
AutoReverse = false,
};
brush.BeginAnimation(SolidColorBrush.ColorProperty, ca1);
Comments
-
Eric Smith about 4 years
I'm trying to fade in a new control to my application's "app" area which is programmatically added after the existing controls are removed. My code looks like this:
void settingsButton_Clicked(object sender, EventArgs e) { ContentCanvas.Children.Clear(); // Fade in settings panel NameScope.SetNameScope(this, new NameScope()); SettingsPane s = new SettingsPane(); s.Name = "settingsPane"; this.RegisterName(s.Name, s); this.Resources.Add(s.Name, s); Storyboard sb = new Storyboard(); DoubleAnimation settingsFade = new DoubleAnimation(); settingsFade.From = 0; settingsFade.To = 1; settingsFade.Duration = new Duration(TimeSpan.FromSeconds(0.33)); settingsFade.RepeatBehavior = new RepeatBehavior(1); Storyboard.SetTargetName(settingsFade, s.Name); Storyboard.SetTargetProperty(settingsFade, new PropertyPath(UserControl.OpacityProperty)); ContentCanvas.Children.Add(s); sb.Children.Add(settingsFade); sb.Begin(); }
However, when I run this code, I get the error "No applicable name scope exists to resolve the name 'settingsPane'."
What am I possibly doing wrong? I'm pretty sure I've registered everything properly :(