次の方法で共有


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

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

Placement プロパティが Customに設定されている場合、PopupCustomPopupPlacementCallback デリゲートの定義済みインスタンスを呼び出します。 このデリゲートは、ターゲット領域の左上隅と 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)

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

関連項目