views:

596

answers:

2

I'm trying to place a control that I have created on my canvas. The idea is to be able to dynamically add them on the fly. Like on a button click or at the end of a DispatchTimer. I have the following, but it doesn't work:

    FirstCircleControl mc = new FirstCircleControl();
    Canvas.SetLeft(mc, 100);
    Canvas.SetTop(mc, 100);

I don't see any control appear...

+3  A: 

You need to add the control to the Canvas first.

yourCanvas.Children.Add(mc)
sipwiz
+2  A: 

Placing a control inside a canvas or grid is a two-step process.

  1. Add the control to the container's child collection
  2. Set the control's location within the container

You've got the 2nd step, but are missing the first.

For a canvas

Button childButton = new Button();
LayoutCanvas.Children.Add(childButton);
Canvas.SetLeft(childButton, 120);
Canvas.SetTop(childButton, 120);

For a grid

Button childButton = new Button();
LayoutGrid.Children.Add(childButton);
Grid.SetRow(childButton, 2);
Grid.SetColumn(childButton, 2);
Brad Tutterow
Thanks Brad. I noticed I also needed to set the width and height of my control to make it visible.
Ry