Popup with placement mode as relative renders in wrong position on some machines.

Ganesha moorthy 6 Reputation points
2021-01-28T06:26:23.99+00:00

Hi Team,

WPF Popup with placement mode as relative renders in wrong position on some machines. As per the behavior it must be rendered on the left top corner and inside the placement target layout, but it renders outside the placement target layout. Please refer the following image and machine configuration,

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/popup-placement-behavior?view=netframeworkdesktop-4.8#how-the-properties-work-together

61333-error-popup-wpf-image.png

Machine configuration:

  • Laptop model: Dell Latitude 3490 and Lenovo Yoga 510
  • OS: Windows 10 Pro
  • .Net Framework: 4.7 and 4.8
  • Visual studio: 2017 and 2019

On some machines it works as per the behavior, left top corner and inside the placement target layout. Please refer the following image and machine configuration.

61267-correct-popup-wpf-image.png

Machine configuration

  • Laptop model: Dell Latitude 5501 and ASUS VivoBook 14 X409FA-EK555T
  • OS: Windows 10 Pro
  • .Net Framework: 4.7 and 4.8
  • Visual studio: 2017 and 2019

Code snippet:

Xaml

<Grid x:Name="innerLayout" Background="Yellow" HorizontalAlignment="Center" VerticalAlignment="Center" Height="300" Width="400" />  

C#

public partial class MainWindow : Window  
{  
    private Popup relativePopup;  
    private TextBlock textBlock;  
    private UserControl contentcontrol = new UserControl();  
    public MainWindow()  
    {  
        InitializeComponent();  

        this.relativePopup= new Popup();  
        this.ShowPopupOnInitialLoad();  
    }  

    internal virtual void ShowPopupOnInitialLoad()  
    {  
        this.relativePopup.AllowsTransparency = true;  
        this.relativePopup.PlacementTarget = this.innerLayout;  
        this.relativePopup.Placement = PlacementMode.Relative;  
        this.relativePopup.Height = 50;  
        this.relativePopup.Width = 100;  
        this.relativePopup.SnapsToDevicePixels = true;  

        if (textBlock == null)  
        {  
            textBlock = new TextBlock();  
            textBlock.Text = "Relative Popup";  
            textBlock.Background = Brushes.Blue;  
            textBlock.Foreground = Brushes.White;  
            textBlock.SnapsToDevicePixels = true;  
            contentcontrol.Content = textBlock;  
        }  

        this.relativePopup.Child = contentcontrol;  
        textBlock.Height = 50;  
        textBlock.Width = 100;  
        this.relativePopup.IsOpen = true;  
    }  
}  

Could you please check and provide a solution for this?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,795 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.