WPF FAQ - Popup

Find answers for the most frequently asked questions
Expand All Collapse All

You can specify a custom pop-up location by setting the Placement property to Custom. This can be done with the following code snippets.

[XAML]

<Popup Name="myCustomPopup" 
        PlacementTarget ="{Binding ElementName=myGridLayout}"
        Placement="Custom">
  <TextBlock Height="50" Width="150"
             Background="LightGray"
             TextWrapping="Wrap">Custom popup position demo</TextBlock>
</Popup>
  
[C#]
public CustomPopupPlacement[] placementForPopup(Size popupSize,
                                           Size targetSize,
                                           Point offset)
{
    CustomPopupPlacement customPlacement1 =
       new CustomPopupPlacement(new Point(-40, 90), PopupPrimaryAxis.Vertical);
  
    CustomPopupPlacement customPlacement2 =
        new CustomPopupPlacement(new Point(20, 30), PopupPrimaryAxis.Horizontal);
  
    CustomPopupPlacement[] customPlacements =
            new CustomPopupPlacement[] { customPlacement1 , customPlacement2  };
    return customPlacements ;
}
  
[C#]
myCustomPopup.CustomPopupPlacementCallback =
    new CustomPopupPlacementCallback(placementForPopup);

Reference link: https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-specify-a-custom-popup-position

Permalink

In order to rotate the Popup, this example assigns a RotateTransform to the ‘RenderTransform’ property on the Canvas, which is the child element of the Popup. For the transform to work correctly, the example must set the ‘AllowsTransparency property’ to true. In addition, the Margin on the Canvas content must specify enough space for the Popup to rotate.

[XAML]

<Popup IsOpen='{Binding ElementName=myCheckBox,Path=IsChecked}' 
       PlacementTarget='{Binding ElementName=myCheckBox}'            
       AllowsTransparency='True'
       PopupAnimation='Slide'
       HorizontalOffset='50'
       VerticalOffset='50'
       >
  <!--The Margin set on the Canvas provides the additional 
      area around the Popup so that the Popup is visible when 
      it rotates.-->
  <Canvas Width='100' Height='100' Background='DarkBlue'
          Margin='150'>
    <Canvas.RenderTransform>
      <RotateTransform x:Name='theTransform' />
    </Canvas.RenderTransform>
    <TextBlock TextWrapping='Wrap' Foreground='White'>
      Rotating Popup
    </TextBlock>
  </Canvas>
</Popup>

Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.