UserControl Location in WPF

前端 未结 2 1241
离开以前
离开以前 2021-01-20 15:04

I am new in WPF, I created a new UserControl MyUserControl.

Now I am surprised: the UserContol does not have a location.

How can I read (by code) myUserCont

2条回答
  •  迷失自我
    2021-01-20 15:55

    The WPF layout system doesn't use absolute positioning, unless you're placing your controls on a container that supports absolute positioning (typically a Canvas). If you're using a Canvas, you can get or set the position of the control using the Canvas.Left, Canvas.Right, Canvas.Top and Canvas.Bottom attached properties:

    double x = Canvas.GetLeft(myControl);
    double y = Canvas.GetTop(myControl);
    

    Now, if you want the actual location of the control (relative to its parent), you can use the VisualTreeHelper.GetOffset method:

    Vector offset = VisualTreeHelper.GetOffset(myControl);
    double x = offset.X;
    double y = offset.Y;
    

提交回复
热议问题