1

How can I create an instance of my UserControl dynamically (in code behind) and display it on the Canvas?

1 Answer 1

2
YourUserControlClassName myUC = new YourUserControlClassName();
myCanvas.Children.Add(myUC);

If myCanvas is not dynamic and is in xaml then you need this to get the reference myCanvas

Canvas myCanvas = (Canvas)this.FindName("CanvasNameInXaml");
Sign up to request clarification or add additional context in comments.

2 Comments

@JooLio: Although this solves your problem, this is a bad approach. If you portray the scenario better, I would try to suggest a better design.
Canvas has Name attribute. I can use it in my code by name. But I didn't know I can get control by it's String name o_O. That may be very usefull.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.