Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a function in C# to draw an Ellipse to a canvas (canvas1) that I have created in a separate Window1.xaml file. My problem arises when I try to add the Ellipse(node) to the canvas via:

canvas1.Children.Add(node);


canvas1 does not exist in the current context. I need to know how to access canvas1 in this context.
Posted
Updated 17-Apr-10 7:21am
v2

If your function is being called from the code behind of Window1 you can pass the canvas as a parameter to your function.

C#
public void AddEllipse(Canvas canvas)
{
    ...
    ...

    canvas.Children.Add(node);
}


If your function is being called from some other code you can expose the canvas as a public property of Window1.

C#
public Canvas Canvas1
{
    get
    {
        return canvas1;
    }
}


Then through any instance of Window1 you can access the Canvas1 property and pass it to your function.
 
Share this answer
 
I would create the Canvas object in question as a static (and globally accessible) object, and just use the static instance wherever it's needed.
 
Share this answer
 
I'm not sure if this is the proper method of asking questions about answers, but Thank you for your responses.

Rajitha, your first solution has fixed the related errors thank you!

John, how should I go about making the canvas static and global in XAML?
 
Share this answer
 

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