Name cannot Found InvalidOperationException

Posted by Greendragons under WPF on 3/10/2011 | Points: 10 | Views : 2349 | Status : [Member] | Replies : 0
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 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); //here the error is, Name cannot be found in Canvas...
dAnim.SetValue(Storyboard.TargetPropertyProperty, new PropertyPath(Canvas.TopProperty));

MyStoryBoard.Children.Add(dAnim);

}


}

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


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


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




Responses

(No response found.)

Login to post response