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

Viewed 35064

Does anyone know how I can place a Popup Control in the Center of the screen?

Thanks!

5 Answers

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>

You can use Placement="Center" and find ancestor Window using RelativeSource and set "PlacementTarget" property as property like this:

         <Popup
            Placement="Center"
            PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
            PopupAnimation="Slide">
               <ContentControl Content="{Binding Yourcontent}"/>
         </Popup>

And to keep stupid comments refrained: I know this centers the popup in the middle of the parent window (not neccessarily middle of the screen [if the parent window is not centered on the screen, what really is not regular case]) but this shall statisfy in most of the cases.

Related