Hello and thanks for your help.
I have a window with a stackpanel named stackpanel and I have added some custom controls to it in xaml which works great.
Now my program dynamically creates some user controls (with buttons) inside the stackpanel. I want to be able to click a button inside my user control and dynamically add another control to the stackpanel.
I can’t seem to get this to work since it is in a different class. How can I add a control to the stackpanel from another control? I couldn’t figure out how to use directcast either.
Thank you!
The stackpanel needs to offer a method (let’s call it myCreateMethod in this example) to create the control and the method must be accessible by the user control. When you create the user control, pass it a handle to your stackpanel, so e.g.
Inside your user control you store the reference:
And then you can call the method to create the control like this:
I hope that gives enough insight to adapt the example to your needs.
EDIT: You could obviously pass details about the new control as parameter(s) to myCreateMethod(). Or you create the new control in the user control, pass it along the the stackpanel and myCreateMethod() “only” displays it.