You need to Register the name before you use it for any other control.
public void CreateRandomStars()
{
int Width = (int)MyCanvas.Width;
int Height = (int)MyCanvas.Height;
Random Rnx = new Random(0);
for (int i = 0; i < 300; i++)
{
double Lx = (double)Rnx.Next(Width);
double Ty = (double)Rnx.Next(Height);
Ellipse es = new Ellipse();
es.Name = "es_" + i.ToString();
es.Width = 2;
es.Height = 2;
es.Fill = Brushes.WhiteSmoke;
es.SetValue(Canvas.LeftProperty, Lx);
es.SetValue(Canvas.TopProperty, Ty);
MyCanvas.Children.Add(es);
this.RegisterName(es.Name, es);
DoubleAnimation dAnim = new DoubleAnimation();
dAnim.From = Ty;
dAnim.To = Height;
dAnim.Duration = new Duration(new TimeSpan(0, 0, 5));
Storyboard.SetTargetName(dAnim, es.Name);
Storyboard.SetTargetProperty(dAnim, new PropertyPath(Canvas.TopProperty));
MyStoryBoard.Children.Add(dAnim);
}
}
The RegisterName is necessary when you want other object to refer your dynamically created element.
I hope this will work for you.