Skip to main content

Microsoft Silverlight

Answered Question Position of UIElement RSS Feed

(0)

v_georgiev
v_georgiev

Member

Member

4 points

13 Posts

Position of UIElement

In Windows Forms, when I set the position of an UIElement I do it that way:
 

UIElement.Location = new Point(X, Y);

// then I get it with

UIElement.Location.X
UIElement.Location.Y
 How can I do this simple task in Silverlight, for example with canvas, called "MyCanvas";

 // This doesn' t work.

MyCanvas.Margin.Left
MyCanvas.Margin.Top

What is this Margin about?

Pravinkumar R. D.
Pravinku...

Contributor

Contributor

4300 points

708 Posts

Answered Question

Re: Position of UIElement

Hi,

You can set the position of the controls within canvas using

Canvas.SetLeft(object,value);

Canvas.SetTop(object,value);

Check the below articals as well-

http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-2-using-layout-management.aspx

http://msdn.microsoft.com/en-us/library/cc189087(VS.95).aspx

Thanks,

Pravin

"Please mark as answered, if this answers your question"

amyo
amyo

Contributor

Contributor

3630 points

495 Posts

Re: Position of UIElement

 You can use the SetValue method to set the Canvas Left (X) and Top(Y) property

Sample:

    <Canvas x:Name="LayoutRoot" Background="White">
        <Canvas x:Name="myCanvas"></Canvas>
    </Canvas>
   
            //X
            myCanvas.SetValue(Canvas.LeftProperty, 20);

            //Y
            myCanvas.SetValue(Canvas.TopProperty, 20);

            //Above X, Y of myCanvas relative to LayoutRoot
 

Amyo Kabir
Solution Architect & Sr. Developer
Blog

v_georgiev
v_georgiev

Member

Member

4 points

13 Posts

Re: Position of UIElement

And how can I get these values in double, because GetValue gives me object?

jeetumaker
jeetumaker

Member

Member

525 points

92 Posts

Re: Re: Position of UIElement

to get values in double either use the Convert.ToDouble or the type cast - for eg: In C# you can use :

double x = (double) objectVariable

I hope this helps. Please click on the "Mark as Answer" if it has answered your question.

Thanks
Jeetu Maker
http://jeetumaker.blogspot.com
(Light_up_your_apps) using Silverlight.2.0;
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities