Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have been checking how to set the position of multiple user controls generated dynamically.

Some links talk about use "TransformToAncestor" but all the examples only explain how to get the current control location with it , instead of set it when you create the control.

Other links recommend insert them into a canvas and use Canvas properties ( Canvas.Left , Canvas.Top,...). I did a small test and this is not working for me:

C#
Test testcontrol = new Test(trucker);
               int left = 5;
               int top = 5;
               for (int y = 0; y < 5; y++)
               {
                   Canvas.SetLeft(testcontrol, left); //set widget position
                   Canvas.SetTop(testcontrol, top);//set widget position
                   this.c1.Children.Add(t);
                   left += 5 * 5;
                   top += 5 * 5;
               }


Basically I was trying to generate on my c1 canvas 5 test controls and at incremental positions assigned. And doesn't works.

Also this solution doesn't fit with my objective very well. I would like to bind the position of the test controls ( generated dynamically in code behind) with X,Y values.
Is there more properly solution for locate user controls by x,y values?
Posted
Updated 26-Oct-14 23:30pm
v2

Here is what you do: you create a single instance of control; its reference is testcontrol. Then you move it on canvas in 5 different positions. No wonder, the result of first moves is forgotten, you leave the control in the last position. Only the last (left bottom) position counts and will be shown.

What would you expect?

One line is unclear:
C#
this.c1.Children.Add(t);

As t definition is not shown in your code sample, I don't know what is it; and don't even know if this code compiles. If it compiles, this is a variable or a member declared somewhere else. Anyway, adding some UIElement to the same parent element in logical tree should not work…

—SA
 
Share this answer
 
Comments
Kinna-10626331 27-Oct-14 5:46am    
Thanks for your answer related to my bug , at the same time we publish both xD. I just discover how to do my objetive. And yes the code compiles , and added t contyrol only once (not five , probabily in the same position).
Sergey Alexandrovich Kryukov 27-Oct-14 5:52am    
Then your question was unclear, it looks like you were talking about Canvas only.
—SA
I found the solution , very easy , I can set the position inside of the control using margin property. It is the current equivalent in WPF.

So at Test class , in the constructor I can declare :
this.Margin = new Thickness(X, Y, 0, 0);

and bind here myvalues as I prefer.

I figure it checking this previous question:

control position in wpf[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900