C#/WPF: Place Popup Control in Center of Screen? C#/WPF: Place Popup Control in Center of Screen? wpf wpf

C#/WPF: Place Popup Control in Center of Screen?


Use the Placement and PlacementTarget properties to position it relative to whatever panel is at the root of the window. So if I have a Grid, StackPanel, etc. that contains all the other "stuff" in the window called MainPanel, I do something like:

<Popup    PlacementTarget="{Binding ElementName=MainPanel}"    Placement="Center">


First, you can use the static properties FullPrimaryScreenHeight, FullPrimaryScreenWidth of the System.Windows.SystemParameters class to get the height and width of the screen. Then, you can set the Top and Left properties of your Popup Control using the width and height before showing it.

Something like.

double primScreenHeight = System.Windows.SystemParameters.FullPrimaryScreenHeight;double primScreenWidth = System.Windows.SystemParameters.FullPrimaryScreenWidth;_yourControl.Top = (primScreenHeight - _yourControl.Height) / 2;_yourControl.Left = (primScreenWidth - _yourControl.Width) / 2;


Use Grid as container and Alignment will work fine for you:

<Popup IsOpen="True">  <Grid Name="canvasMain">    <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">      ...    </StackPanel>  </Grid></Popup>