UserControl Location in WPF

前端 未结 2 1243
离开以前
离开以前 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:50

    Elements (like user controls) are normally placed in panels in WPF. Depending on which panel you are using the panel may add some attached properties to the user control. If the user control is placed in a Canvas it will get the attached properties Left, Top, Right and Bottom. However, if the user control is placed in a Grid it will get the attached properties Row and Column (and some more). Other panels like StackPanel will not attach any properties. There is no such thing as a universal user control location.

    Panels Overview

    Attached Properties Overview

    Assuming that you are using a Canvas as your panel you can access the attached Left and Top properties like this:

    Double x = Canvas.GetLeft(myUserControl1);
    Double y = Canvas.GetTop(myUserControl1);
    

提交回复
热议问题