在我的 XAML 中,我有類似的內容:
<client:View ...
...
<controls:Location Canvas.Left="169500"
Canvas.Top="52610"
LocationName="Location_Name"
Rotation="0" MouseDoubleClick="Location_MouseDoubleClick"/>
ALocation是 的子類,如您所見:
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?
有人有想法嗎?
提前致謝
uj5u.com熱心網友回復:
您很可能正在尋找一種如何閱讀attached property.
讀取附加屬性(或任何依賴屬性)可以GetValue使用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
}
或者沒有所有這些安全演員
var canvasLeft = (double)((DependencyObject)sender).GetValue(Canvas.LeftProperty);
GetValue 檔案:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.dependencyobject.getvalue
注意:
代碼使用最新c#的語言語法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/440628.html
