Link to home
Start Free TrialLog in
Avatar of tonysebsatian
tonysebsatian

asked on

Access UserControl.left and UserControl.top c#

How can I access the user controls left and top properties from within the user control?
I have a WPF user control. When i create this control dynamically through code i cannot access top and left properties. Any help regarding this will be much appreciated.. thanks very much
Avatar of tculler
tculler
Flag of United States of America image

Can you post the error message, or summarize it?
Avatar of tonysebsatian
tonysebsatian

ASKER

Thanks for your response...

I have a requirement where I need to create a rectangle shape when clicked a button. I created a WPF usercontrol as WinForm application dont have a rectangle or a shape control.

// When I create an instance of a rectangle object (usercontrol)
Rectangle rectangle = new Rectangle();
//and pass it to the below constructor of a class, it gives me all the properties and methods except Top and Left
 public RectangleElement(Rectangle rectangle)
        {
            height= rectangle.Height;
            width = rectangle.Width;
            top = 0;//   cannot find rectangle.Top
            left = 0;//   cannot find rectangle.Left
                    }

Hope this give a better view of my problem...  Thanks very much once again....
Hello, I think I have found a way to get Top and Left  values. and it seems working...

I added the below code snippet to the Rectangle class...
And i am able to move the rectangle using top and Left values..

Thaniks very much for your time and effort....

       public double Top
        {
            get { return Convert.ToDouble(Canvas.TopProperty); }
            set { this.SetValue(Canvas.TopProperty, value); }
        }
 
 
        public double Left
        {
            get { return Convert.ToDouble(Canvas.LeftProperty); }
            set { this.SetValue(Canvas.LeftProperty, value); }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of tculler
tculler
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your comment..
The WPF Rectangle control has both Top and left properties.
But when I add rectangle to a WPF user control and create an instance of it  --
WPFUserControl myusercontrol = new WPFUserControl();
I cant find both Top and Left properties.  But I have resolved this by adding two peoperties, Top and Left to the UserControl as i have listed in my previous message

Yes, I am taking your advice regarding Double. I have changed it to Int(int32) as its usually used. Thanks for the advice again...
Thanks for your help....
Hello, thanks very much for your time. Thought you didnt had to spend much time on this Issue,  I would like to give you all the credits as an appreciation for your time...  Thanks
I apologize for not being moer help, I've been a bit busy as of late, and have been slower on getting back to questions because of it. I'm glad you've found a solution though :)

Nate