Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Im getting Exception that name of ellipse which is dynamically set is not found while setting StoryBoard.TargetName of DoubleAnimation....
I already have StoryBoard in my XAML...
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);
               starPoints.Add(Ty);
               Ellipse es = new Ellipse();
               es.Name = "es_" + i.ToString(); //setting names dynamically for each
               //ellipse

               es.Width = 2;
               es.Height = 2;

               es.Fill = Brushes.WhiteSmoke;
               es.SetValue(Canvas.LeftProperty, Lx);
               es.SetValue(Canvas.TopProperty, Ty);

               MyCanvas.Children.Add(es);

               DoubleAnimation dAnim = new DoubleAnimation();
               dAnim.From = Ty;
               dAnim.To = Height;
               dAnim.Duration = new Duration(new TimeSpan(0, 0, 5));
               dAnim.SetValue(Storyboard.TargetNameProperty, es.Name); //Getting Error
               dAnim.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(Canvas.TopProperty));

               MyStoryBoard.Children.Add(dAnim);

           }


       }


{"'es_0' name cannot be found in the name scope of 'System.Windows.Controls.Canvas'."}
This is the error...

XAML code

XML
<Canvas Name="MyCanvas" Background="Black" Height="310" Width="500">
        <Canvas.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard RepeatBehavior="Forever" Name="MyStoryBoard">
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Canvas.Triggers>
    </Canvas>
Posted

I changed your XAML to below and I didn't got any exception and the stars showed up.

XML
<EventTrigger RoutedEvent="Canvas.Loaded">


Here I am calling CreateRandomStars() method from MyCanvas's Loaded event.

But when CreateRandomStars() method is called from Window's Loaded event, I am getting the error message. It is simple to understand. The Children in the Canvas is not ready when Window's Loaded event is fired. So, call the method in MyCanvas's Loaded event and change the XAML as I mentioned above.

Mark it as answer, if it is helpful.
 
Share this answer
 
Comments
greendragons 9-Mar-11 23:07pm    
Sorry im new to WPF,
Im still getting error, even i changed ma XAML...maybe im making the call from wrong place...

private void MyCanvas_Loaded(object sender, RoutedEventArgs e)
{
CreateRandomStars();
}
Venkatesh Mookkan 9-Mar-11 23:12pm    
Upload your sample project somewhere like http://skydrive.live.com or DropBox and give the Url. I will check and let you know.
greendragons 9-Mar-11 23:30pm    
this is the link.... http://jumbofiles.com/vq0v7vorhfip
And yes stars are showing up...but i want them to move from their original Canvas.top position to
the bottom limit of canvas....tht is why im using DoubleAnimation...
Thnx!
greendragons 10-Mar-11 9:09am    
I have changed to
EventTrigger RoutedEvent="Canvas.Loaded"

and
.cs file

public partial class MainWindow : Window
{
public List<double> starPoints = new List<double>();

public MainWindow()
{
InitializeComponent();


}

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);
starPoints.Add(Ty);
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);

DoubleAnimation dAnim = new DoubleAnimation();
dAnim.From = Ty;
dAnim.To = Height;
dAnim.Duration = new Duration(new TimeSpan(0, 0, 5));
//dAnim.SetValue(Storyboard.TargetNameProperty, es.Name);
dAnim.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(Canvas.TopProperty));

MyStoryBoard.Children.Add(dAnim);

}


}

private void MyCanvas_Loaded(object sender, RoutedEventArgs e)
{
CreateRandomStars();
}

}
Dalek Dave 10-Mar-11 18:37pm    
Worth a 5
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);
                //starPoints.Add(Ty);
                Ellipse es = new Ellipse();
                es.Name = "es_" + i.ToString(); //setting names dynamically for each
                //ellipse

                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));
                //StoryBoard.SetValue(Storyboard.TargetNameProperty, es.Name); //Getting Error
                //dAnim.SetValue(Storyboard.TargetPropertyProperty, 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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900