How to: Specify a Custom Popup Position
This example shows how to specify a custom position for a Popup control when the Placement property is set to Custom.
Example
When the Placement property is set to Custom, the PlacementRectangle, VerticalOffset, and HorizontalOffset properties have no effect. Instead, the Popup calls a defined instance of the CustomPopupPlacementCallback delegate. This delegate returns a set of possible points that are relative to the target element where you want the Popup to appear. Popup placement occurs at the point that provides the best visibility.
The following example shows how to define the position of a Popup by setting the Placement property to Custom. It also shows how to create and assign a CustomPopupPlacementCallback delegate in order to position the Popup.
<Popup Name="myPopup"
PlacementTarget ="{Binding ElementName=myButton}"
Placement="Custom">
<TextBlock Height="60" Width="200"
Background="LightGray"
TextWrapping="Wrap">Popup positioned by using
CustomPopupPlacement callback delegate</TextBlock>
</Popup>
public CustomPopupPlacement[] placePopup(Size popupSize,
Size targetSize,
Point offset)
{
CustomPopupPlacement[] ttplaces =
new CustomPopupPlacement[] { new CustomPopupPlacement() };
ttplaces[0].Point = new Point(-50,90);
ttplaces[0].PrimaryAxis = PopupPrimaryAxis.Vertical;
return ttplaces;
}
myPopup.CustomPopupPlacementCallback =
new CustomPopupPlacementCallback(placePopup);
For the complete sample, see Custom Popup Placement Sample.