How do you dynamically place a control on a canvas in Silverlight?

I am trying to install the control that I created on mine Canvas. The idea is to dynamically add them on the fly. Like at the touch of a button or at the end DispatchTimer. I have the following, but it does not work:

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

I do not see any controls ...

+5
source share
3 answers

First you need to add the control to Canvas.

yourCanvas.Children.Add(mc)
+6
source

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

  • Add a control to the container's child collection
  • Set the management location in the container

, .

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

Button childButton = new Button();
LayoutGrid.Children.Add(childButton);
Grid.SetRow(childButton, 2);
Grid.SetColumn(childButton, 2);
+6

Another (easier in some cases) way to do this from the side UIElement:

    (controlItem as UIElement).SetValue(Canvas.TopProperty, topVal);
    (controlItem as UIElement).SetValue(Canvas.LeftProperty, left);
+1
source

All Articles