Partager via


Comment : spécifier une position de menu contextuel personnalisée

Cet exemple montre comment spécifier une position personnalisée pour un contrôle Popup lorsque la propriété Placement a la valeur Custom.

Exemple

Lorsque la propriété Placement a la valeur Custom, le Popup appelle une instance définie du délégué CustomPopupPlacementCallback. Ce délégué retourne un jeu de points possibles qui sont relatifs au coin supérieur gauche de la zone cible et au coin supérieur gauche du Popup. L'emplacement du Popup a lieu au point qui fournit la meilleure visibilité.

L'exemple suivant montre comment définir la position d'un Popup en affectant Custom à la propriété Placement. Il indique également comment créer et assigner un délégué CustomPopupPlacementCallback afin de positionner le Popup. Le délégué de rappel retourne deux objets CustomPopupPlacement. Si le Popup est masqué par un bord d'écran à la première position, le Popup est placé à la deuxième position.

 <Popup Name="popup1"  
        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 Function placePopup(ByVal popupSize As Size, ByVal targetSize As Size, ByVal offset As Point) As CustomPopupPlacement()
            Dim placement1 As New CustomPopupPlacement(New Point(-50, 100), PopupPrimaryAxis.Vertical)

            Dim placement2 As New CustomPopupPlacement(New Point(10, 20), PopupPrimaryAxis.Horizontal)

            Dim ttplaces() As CustomPopupPlacement = { placement1, placement2 }
            Return ttplaces
        End Function
public CustomPopupPlacement[] placePopup(Size popupSize,
                                           Size targetSize,
                                           Point offset)
{
    CustomPopupPlacement placement1 =
       new CustomPopupPlacement(new Point(-50, 100), PopupPrimaryAxis.Vertical);

    CustomPopupPlacement placement2 =
        new CustomPopupPlacement(new Point(10, 20), PopupPrimaryAxis.Horizontal);

    CustomPopupPlacement[] ttplaces =
            new CustomPopupPlacement[] { placement1, placement2 };
    return ttplaces;
}
            popup1.CustomPopupPlacementCallback = New CustomPopupPlacementCallback(AddressOf placePopup)
popup1.CustomPopupPlacementCallback =
    new CustomPopupPlacementCallback(placePopup);

Pour obtenir l'exemple complet, consultez Positionnement de Popup, exemple.

Voir aussi

Référence

Popup

Concepts

Vue d'ensemble de Popup

Autres ressources

Rubriques "Comment" relatives aux menus contextuels