Thursday, November 21, 2013

Get Absolute Position of element within the window in wpf

You have to specify window you clicked in Mouse.GetPosition(IInputElement relativeTo) Following code works well for me
protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        base.OnMouseDown(e);
        Point p = e.GetPosition(this);
        MessageBox.Show(p.ToString());
    }
I suspect that you need to refer to the window not from it own class but from other point of the application. In this case Application.Current.MainWindow will help you.

Reference:
http://stackoverflow.com/questions/386731/get-absolute-position-of-element-within-the-window-in-wpf

No comments: