如何获取 WPF PresentationFramework 对象的坐标?

How to get the coordinates of a WPF PresentationFramework object?

在我的 XAML 中,我有类似的东西:

<client:View ...
    ...
    <controls:Location Canvas.Left="169500"
                       Canvas.Top="52610"
                       LocationName="Location_Name"
                       Rotation="0" MouseDoubleClick="Location_MouseDoubleClick"/>

A Location 是 的子类,如您所见:

using System.Windows.Controls;
...
public class Location : UserControl ...

在对应的*.xaml.cs中,我有:

    private void Location_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        <namespace>.Location Loc = sender as <namespace>.Location;

        Point location_Point = Mouse.GetPosition(this);
        // This gives the X and Y coordinates of the mouse pointer.
        // I would like to know the X and Y coordinates of the Location object,
        // without needing to pass via a mouse event, such as:
        Loc. // but what attributes/properties contain that information?

有人知道吗?
提前致谢

您最有可能正在寻找一种方法来阅读 attached property

读取附加的 属性(或任何依赖项 属性)可以使用 DependendyObjectGetValue 方法。

//first cast your sender to a DependencyObject
if (sender is DependencyObject dpObj)
{
    //GetValue will return an object
    //Canvas has a static member for the attached property LeftProperty
    var value = dpObj.GetValue(Canvas.LeftProperty);
    
    //cast value to double
    if (value is double canvasLeft)
    {
        //do something with canvasLeft
    }
    else
    {
        //do something if the value isn't a double, should never be the case
    }
}
else
{
    //do something if sender isn't a DependencyObject
}

或者没有所有那些安全的转换

var canvasLeft = (double)((DependencyObject)sender).GetValue(Canvas.LeftProperty);

GetValue 文档:

https://docs.microsoft.com/en-us/dotnet/api/system.windows.dependencyobject.getvalue

通知:

代码使用最新的 c# 语言语法。