如何:指定自定义 Popup 位置
更新:2007 年 11 月
下面的示例演示当 Placement 属性设置为 Custom 时,如何为 Popup 控件指定自定义位置。
示例
当 Placement 属性设置为 Custom 时,Popup 会调用为 CustomPopupPlacementCallback 委托定义的实例。此委托返回一组可能的点,这些点相对于目标区域的左上角和 Popup 的左上角。Popup 位置出现在可提供最佳可见性的点。
下面的示例演示如何通过将 Placement 属性设置为 Custom 来定义 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;
}
popup1.CustomPopupPlacementCallback =
new CustomPopupPlacementCallback(placePopup);
有关完整示例,请参见 Popup 位置示例。