我正在尝试获取弹出控件的X,Y坐标。我已经尝试过:
VisualTreeHelper.GetOffset(Popup);
但是返回的向量始终包含X和Y的值为0。
弹出窗口的父级是布局根,即网格。
CustomPopupPlacementCallback的Point参数始终返回0,0。
目标是允许用户将弹出窗口拖动到屏幕上的任何位置。我想通过获取当前弹出窗口和鼠标位置,并朝着鼠标移动的方向移动弹出窗口来实现这一目标。
--------------------更新--------------------
Chris Nicol,我已经尝试了您的答案,并使用以下代码,但仍然收到rootPoint的0,0:
Xaml:
VisualTreeHelper.GetOffset(Popup);
但是返回的向量始终包含X和Y的值为0。
弹出窗口的父级是布局根,即网格。
CustomPopupPlacementCallback的Point参数始终返回0,0。
目标是允许用户将弹出窗口拖动到屏幕上的任何位置。我想通过获取当前弹出窗口和鼠标位置,并朝着鼠标移动的方向移动弹出窗口来实现这一目标。
--------------------更新--------------------
Chris Nicol,我已经尝试了您的答案,并使用以下代码,但仍然收到rootPoint的0,0:
Xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Test.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="800" Height="600">
<Grid x:Name="LayoutRoot">
<Popup x:Name="Popup" IsOpen="True" Placement="Center" Width="100" Height="100">
<Button Click="Button_Click" Content="Test" />
</Popup>
</Grid>
代码后台:
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
}
private void Button_Click(object sender, RoutedEventArgs e)
{
GeneralTransform transform = Popup.TransformToAncestor(LayoutRoot);
Point rootPoint = transform.Transform(new Point(0, 0));
}
}