共用方式為


如何:指定自訂 Popup 的位置

這個範例示範當 屬性設定 Custom 為 時 Placement ,如何指定控制項的 Popup 自訂位置。

範例

Placement當 屬性設定為 Custom 時,會 Popup 呼叫委派的 CustomPopupPlacementCallback 已定義實例。 此委派會傳回一組與目的地區域左上角和 左上角 Popup 相對的可能點。 Popup位置會在提供最佳可見度的點發生。

下列範例示範如何將 屬性設定 PlacementCustom 來定義 的位置 Popup 。 它也會示範如何建立和指派 CustomPopupPlacementCallback 委派,以便放置 Popup 。 回呼委派會傳回兩個 CustomPopupPlacement 物件。 Popup如果 隱藏在第一個位置的螢幕邊緣,則會 Popup 將 放在第二個位置。

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

如需完整的範例,請參閱 快顯放置範例

另請參閱