Determining which CustomPopupPlacement was used for WPF Popup

天涯浪子 提交于 2019-12-01 15:02:58

I have a little hacky solution. Save the custom points as fields in the derived TooTip class and override the OnOpened method.

protected override void OnOpened(RoutedEventArgs e)
{
    base.OnOpened(e);
    var p = this.TranslatePoint(new Point(0, 0), this.PlacementTarget);
    var diff1 = this.first - p;
    var diff2 = this.second - p;

    if (Math.Abs(Math.Min(diff1.Length, diff2.Length) - diff1.Length) < 0.01)
    {
        // First Point
    }
    else
    {
        // Second Point
    }
}

Better solutions are welcome

A better way to find placement of the Popup. This method requires a Child element to be present, but that's no problem considering the Grid that comes with a Popup element.

    UIElement container = VisualTreeHelper.GetParent(this) as UIElement;
    Point relativeLocation = this.Child.TranslatePoint(new Point(0, 0), container); //It HAS(!!!) to be this.Child

    if (relativeLocation.Y < 0) //or use X for left and right
    {
        Console.WriteLine("TOP PLACEMENT!");
    }
    else
    {
        Console.WriteLine("BOTTOM PLACEMENT!");
    }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!