-2

In my XAML, I have something like:

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

A Location is a subclass from , as you can see:

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

In the corresponding *.xaml.cs, I have:

    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?

Does anybody have an idea?
Thanks in advance

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • `sender as .Location;` <-- This is not valid C# – Dai Mar 09 '22 at 15:37
  • Are you asking about WPF `Visual` coordinates (in screen-space, window-space, or some other space?), or the `Canvas.Left`/`Top` dependency-properties? – Dai Mar 09 '22 at 15:38
  • 1
    To read `Canvas.Left` use this: `var canvasLeft = (double)((DependencyObject)sender).GetValue(Canvas.LeftProperty);` | https://learn.microsoft.com/en-us/dotnet/api/system.windows.dependencyobject.getvalue?view=windowsdesktop-6.0 – Rand Random Mar 09 '22 at 15:42
  • @RandRandom: can you write your comment as an answer? I'll accept it. – Dominique Mar 09 '22 at 16:24

1 Answers1

1

You are most likly looking for a way how to read an attached property.

Reading an attached property (or any dependency property) can be with the GetValue method of a DependendyObject.

//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
}

or without all those safe casts

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

GetValue docs:

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

Notice:

Code uses latest c# language syntax.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 1
    Each attached property has a static getter (and setter) method. Better use that, it avoids at least the cast of the return value. So, `var left = Canvas.GetLeft(element)` instead of `var left = (double)element. GetValue(Canvas.LeftProperty)` – Clemens Mar 09 '22 at 17:29
  • I have found following property in the definition of my `UserControl` object: `Canvas.Left="{Binding Other_Object.X}"`, which makes me believe that the `Canvas.Left` and `Canvas.Top` properties are indeed the properties, used to define X and Y coordinates. What I don't understand, is the whole idea behind it: a `UserControl` should have some standard properties for defining its coordinates on screen, why use `attached properties` for such a basic thing? – Dominique Mar 10 '22 at 09:04
  • @Dominique - canvas left and top != the coordination on the screen those properties just define the position of a control inside a canvas, since you showed xaml definition with those properties I assumed you are looking for a way to read those values - if you want to have the XY position of any control, no matter if it is inside a canvas or not have a look at this: https://stackoverflow.com/questions/4095252/how-to-get-xy-coordinates-of-a-control-at-runtime-in-wpf – Rand Random Mar 10 '22 at 11:00