Silverlight 3 中的可拖动弹出窗口
允许用户将弹出窗口拖动到 Silverlight 窗口的任何位置。
引言
在 Silverlight 3 中,您可以使用 Popup
控制来显示包含任何类型内容的非模态窗口。虽然可以通过设置 HorizontalOffset
和 VerticalOffset
属性来控制 Popup
的位置,但有时很难预测用户希望将窗口放置在何处。不幸的是,用户无法移动 Popup
控制。
通过将 PopupDragDrop
对象附加到您的 Popup
控制,您可以允许用户在 Silverlight 窗口中拖动它,并将其放置在任何他们喜欢的位置。
使用代码
只需调用静态 PopupDragDrop.Attach
方法,即可将类的实例连接到您的 Popup
。Attach
接受两个参数:Popup
控制和将作为 Popup
子项的 FrameworkElement
(其中包含将要显示的实际内容)。例如
// Create the Popup control and set the initial position.
Popup myPopup = new Popup { HorizontalOffset = 200, VerticalOffset = 300 };
// Attach a PopupDragDrop object to make your Popup draggable.
// The Attach method will also set the Child property of the Popup.
PopupDragDrop.Attach(myPopup, new myUserControl());
从这一点开始,您可以使用 Popup
的 IsOpen
属性和子项的 Visibility
属性,以通常的方式显示/隐藏 Popup
。
历史
- 2009年11月19日 - 首次贡献。