Partager via


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

Cet exemple montre comment spécifier une position personnalisée pour un Popup contrôle lorsque la Placement propriété est définie Customsur .

Exemple

Lorsque la Placement propriété est définie Customsur , l’appel Popup d’une instance définie du CustomPopupPlacementCallback délégué. Ce délégué retourne un ensemble de points possibles relatifs au coin supérieur gauche de la zone cible et au coin supérieur gauche du Popup. Le Popup placement se produit au point qui offre la meilleure visibilité.

L’exemple suivant montre comment définir la position d’un Popup en définissant la Placement propriété sur Custom. Il montre également comment créer et affecter un CustomPopupPlacementCallback délégué afin de positionner le Popup. Le délégué de rappel retourne deux CustomPopupPlacement objets. Si l’écran Popup est masqué par un bord d’écran à la première position, il 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 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;
}
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
popup1.CustomPopupPlacementCallback =
    new CustomPopupPlacementCallback(placePopup);
popup1.CustomPopupPlacementCallback = New CustomPopupPlacementCallback(AddressOf placePopup)

Pour obtenir l’exemple complet, consultez l’exemple de placement de fenêtre contextuelle.

Voir aussi