I'm trying to get the X,Y coordinates of a popup control. I have tried:
VisualTreeHelper.GetOffset(Popup);
but the vector returned always contains (0,0) for X and Y.
The parent of the popup is the layout root, which is a grid.
The CustomPopupPlacementCallback also always returns 0,0 for it's Point parameter.
The goal is to allow the user to drag the popup anywhere on the screen. I was going to try and accomplish this by getting the current popup and mouse position, and moving the popup in the same direction of the mouse moves.
--------------------Update--------------------
Chris Nicol, I have tried your answer with the following code, but still receive 0,0 for rootPoint:
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>
Code Behind:
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));
}
}