Positioning controls in Code

2 posts, 0 answers
  1. Lindsay Miles
    Lindsay Miles avatar
    75 posts
    Member since:
    Dec 2009

    Posted 20 May 2010 Link to this post

    Hello,

    if we can position controls (buttons in this case) on a canvas using the canvas.top and canvas.left in the xaml for the given button, how can be position buttons dynamically added to a canvas at run-time? 

    Something like:

    Canvas cvs = new Canvas;
    RadButton btn = new RadButton
    {
    Tag = "btnONE", Width = 32, Height = 32, Content = "ONE"
    }
    cvs.Children.Add(btn);

    Lets say I have 10 of these (obviously named/tagged differently) in a loop...the buttons should be 5 rows of 2 in the order they were added - ie: left, right, left, right for 5 rows...

    How can I position each button? What I'm getting in my code is 10 btns on top of each other.

    In xaml I have canvas.top="0" canvas.left="0"
    then in next btn canvas.top="0" canvas.left="32"
    then in next btn canvas.top="32" canvas.left="0"
    then in next btn canvas.top="32" canvas.left="32"

    etc etc etc.

    Make sense?

    Thanks

  2. Lindsay Miles
    Lindsay Miles avatar
    75 posts
    Member since:
    Dec 2009

    Posted 21 May 2010 Link to this post

    Issue Solved.

    Added a StackPanel to RadPane then various Grids and Canvases in the xaml.
    In my C# code, added required rows and set col width and row height etc etc.
    Built the buttons as before and added to relevant Grid or Canvas.

    All works and looks good.

Back to Top