Share via


方法: ポップアップのカスタム位置を指定する

この例では、Placement プロパティが Custom に設定されているときに、Popup コントロールのカスタム位置を指定する方法を示します。

Placement プロパティが Custom に設定されていると、Popup では定義されている CustomPopupPlacementCallback デリゲートのインスタンスが呼び出されます。 このデリゲートでは、ターゲット領域の左上隅と Popup の左上隅を基準として、一連の可能なポイントが返されます。 Popup は、視認性が最もよい場所に配置されます。

次の例では、Placement プロパティを Custom に設定することで Popup の位置を定義する方法が示されています。 また、Popup を配置するために CustomPopupPlacementCallback デリゲートを作成して割り当てる方法も示されています。 コールバック デリゲートでは、2 つの CustomPopupPlacement オブジェクトが返されます。 最初の位置では Popup が画面の端で見えなくなる場合、Popup は 2 番目の位置に配置されます。

 <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)

サンプル全体については、ポップアップ配置のサンプルを参照してください。

関連項目