Determining which CustomPopupPlacement was used for WPF Popup

后端 未结 2 1353
生来不讨喜
生来不讨喜 2021-01-16 23:46

I\'m trying to figure out which of the passed in array of CustomPopupPlacement positions have been used when the popup actually renders. Is there any event to d

相关标签:
2条回答
  • 2021-01-17 00:03

    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!");
        }
    
    0 讨论(0)
  • 2021-01-17 00:20

    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

    0 讨论(0)
提交回复
热议问题