locked
Positioning a UserControl RRS feed

  • Question

  • I'm trying to position my UserControl on my form. To do so I have a method which picks a position for it. I've verified that my method is in fact returning unique positions, but all my user controls are rendered in the exact center of the window. The relevant code looks like this:

     

    MyControl ss = new MyControl();

    System.Drawing.Rectangle ssRect = new System.Drawing.Rectangle(10,10,10,10);

    System.Drawing.Point newLoc = PositionPost(ssRect);

    this.Grid.Children.Add(ss);

    Canvas.SetLeft(ss, newLoc.X);

    Canvas.SetTop(ss, newLoc.Y);

     

    "this" is my main window which has this hierarchy

    DocumentRoot --> Canvas --> Grid. I initially did not have a canvas on there at all, but could determine no way to set the position of a child element on a Grid, so....

    I'm sure this will probably turn out to be trivial, but I've been staring at code so long my eyes are beginning to bleed ;).

     

    Tuesday, August 1, 2006 3:27 AM

Answers

  • Meh...I thought I tried adding children to the Canvas a bunch of times and it didn't work, but apparently I was imagining things. Apologies. If I remove the Grid from the window altogether and just add the children to the Canvas everything is fine.

    Time for something to drink.....

     

    Tuesday, August 1, 2006 3:34 AM
  • Your control is a child of Grid and Grid is a layout contaier for it. Setting Canvas-defiend properties has no effect since the Control is not under layout control of Canvas. Grid is positioning the child according to Grid/Child sizes and Horizontal/VerticalAlignment properties. Default Alignment is Stretch, which degrades to Center if the chidl resists stretching (for example, if it has Width.Height set explicitly). You can use Margin inside Grid to create an empty zone around the the Control - this will work as "positioning".

    In general, Grid does sizing and positioning for you, this is why there is no direct control over it, only via these layout properties. Canvas does not do sizing/positioning of its children, so there are explicit properties allowing to set position.

     

     

    Friday, August 11, 2006 6:56 PM

All replies

  • Meh...I thought I tried adding children to the Canvas a bunch of times and it didn't work, but apparently I was imagining things. Apologies. If I remove the Grid from the window altogether and just add the children to the Canvas everything is fine.

    Time for something to drink.....

     

    Tuesday, August 1, 2006 3:34 AM
  • Your control is a child of Grid and Grid is a layout contaier for it. Setting Canvas-defiend properties has no effect since the Control is not under layout control of Canvas. Grid is positioning the child according to Grid/Child sizes and Horizontal/VerticalAlignment properties. Default Alignment is Stretch, which degrades to Center if the chidl resists stretching (for example, if it has Width.Height set explicitly). You can use Margin inside Grid to create an empty zone around the the Control - this will work as "positioning".

    In general, Grid does sizing and positioning for you, this is why there is no direct control over it, only via these layout properties. Canvas does not do sizing/positioning of its children, so there are explicit properties allowing to set position.

     

     

    Friday, August 11, 2006 6:56 PM