tags:

views:

639

answers:

1

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));
    }
}
+1  A: 

Not sure this is the best way of finding that out, but it does work:

GeneralTransform transform = controlToFind.TransformToAncestor(TopLevelControl);
            Point rootPoint = transform.Transform(new Point(0, 0));
Chris Nicol
Yes, that *is* the best way to do it. I usually do it in a single line: `controlToFind.TransformToAncestor(TopLevelControl).Transform(new Point(0,0))`
Ray Burns
yup, likewise, I do it in one line. I just separated it for the example so that it's easier to understand
Chris Nicol
Thanks for the help so far, check my update if you have time.
gamzu07