Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF
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
 
<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 9-Mar-11 13:47pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I changed your XAML to below and I didn't got any exception and the stars showed up.
 
<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.
  Permalink  
Comments
greendragons at 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 at 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 at 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 at 10-Mar-11 9:09am
   
I have changed to
EventTrigger RoutedEvent="Canvas.Loaded"
 
and
.cs file
 
public partial class MainWindow : Window
{
public List starPoints = new List();
 
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 at 10-Mar-11 18:37pm
   
Worth a 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 334
2 CPallini 275
3 DamithSL 230
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 10 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100