在wpf開發(fā)中,常需要在鼠標(biāo)位置處彈出一個“提示框”(在此就以“提示框”代替吧),通過“提示框”進(jìn)行信息提示或者數(shù)據(jù)操作,如果僅僅是提示作用,使用ToolTip控件已經(jīng)足夠,但是有些是需要在彈出的框中有操作數(shù)據(jù)的功能,如彈出框包含一棵樹或者列表,此時ToolTip就沒法實現(xiàn)了,因為ToolTip只能起到顯示的作用,也許有人會有彈出對話框的形式,這是一種辦法,但是要讓對話框總是顯示在鼠標(biāo)的位置,并且對話框的大小需要根據(jù)顯示內(nèi)容的改變(如樹或列表的展開與收縮)而動態(tài)改變的話,實現(xiàn)起來會比較麻煩。通過進(jìn)一步的探討,原來使用Popup控件是那么的簡單和實用,它在許多方面和ToolTip 控件一樣,也是單一內(nèi)容,但是它可以包含任何WPF元素,該內(nèi)容存儲在Popup.Child屬性中,而不像ToolTip控件存儲在ToolTip.Content屬性中。另外,和ToolTip控件一樣Popup控件也可以延伸出窗口的邊界,可以使用IsOpen屬性顯示或隱藏Popup控件。跟ToolTip相比,Popup會有以下幾個比較重要的特點:
(1)ToolTip可以自動顯示,而Popup控件永遠(yuǎn)不會自動顯示,為了顯示Popup控件必須設(shè)置IsOpen屬性。
(2)ToolTip可以自動消失,但是Popup有點不同的就是,在默認(rèn)情況下,Popup.StaysOen屬性被設(shè)置為True,并且Popup控件會一直顯示,直到顯式地將IsOpen屬性設(shè)置為False。如果將Popup.StaysOpen屬性設(shè)置為False,當(dāng)用戶在其他地方單擊鼠標(biāo)時,Popup控件就會消失。
(3)如果Popup控件的IsOpen屬性設(shè)置為True時,可以通過Popup控件的PopupAnimation屬性設(shè)置彈出方式,Placement屬性設(shè)置彈出位置,如Placement="Mouse",表示在鼠標(biāo)位置處彈出“提示框”
(4)Popup控件不和任何控件相關(guān)聯(lián),無論在哪定義Popup標(biāo)簽都可以。
案例效果:
描述一下問題:就是從上到下滑動,顯示經(jīng)過滑動的彈框,滑動到下一個時,上一個就自動消失了(已完成)?,F(xiàn)在是想進(jìn)入到彈框里面點擊里面的東西。因為我設(shè)置了移開觸發(fā)區(qū)域就彈框消失?,F(xiàn)在我不知道怎么判定一下,讓它離開觸發(fā)區(qū)域,往右移,進(jìn)入到彈框時,彈框不消失。
兩種解決方式:
1.判斷坐標(biāo)點是否在控件范圍內(nèi):

2.用定時器來解決:
引用下命名空間就行了

WPF中Popup的幾個問題



下面是解決popup彈框置頂?shù)膯栴}:
在程序?qū)懸粋€popup發(fā)現(xiàn)他會在置頂,在網(wǎng)上找了兩大神代碼,就使用他的代碼
http://www.cnblogs.com/Leaco/p/3164394.html
http://blog.csdn.net/baijinwen/article/details/6159043
只需要把下面的類放到自己的軟件,然后把使用 popup 替換為 CCPopup 就不會讓popup置頂
public class CCPopup : Popup
? ? {
? ? ? ? public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(typeof(CCPopup), new FrameworkPropertyMetadata(false, OnTopmostChanged));
? ? ? ? public bool Topmost
? ? ? ? {
? ? ? ? ? ? get { return (bool)GetValue(TopmostProperty); }
? ? ? ? ? ? set { SetValue(TopmostProperty, value); }
? ? ? ? }
? ? ? ? private static void OnTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
? ? ? ? {
? ? ? ? ? ? (obj as CCPopup).UpdateWindow();
? ? ? ? }
? ? ? ? protected override void OnOpened(EventArgs e)
? ? ? ? {
? ? ? ? ? ? UpdateWindow();
? ? ? ? }
? ? ? ? private void UpdateWindow()
? ? ? ? {
? ? ? ? ? ? var hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
? ? ? ? ? ? RECT rect;
? ? ? ? ? ? if (GetWindowRect(hwnd, out rect))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? #region P/Invoke imports & definitions
? ? ? ? [StructLayout(LayoutKind.Sequential)]
? ? ? ? public struct RECT? ? ? ? {
? ? ? ? ? ? public int Left;
? ? ? ? ? ? public int Top;
? ? ? ? ? ? public int Right;
? ? ? ? ? ? public int Bottom;
? ? ? ? }
? ? ? ? [DllImport("user32.dll")]
? ? ? ? [return: MarshalAs(UnmanagedType.Bool)]
? ? ? ? private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
? ? ? ? [DllImport("user32", EntryPoint = "SetWindowPos")]
? ? ? ? private static extern int SetWindowPos(IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags);
? ? ? ? #endregion
? ? }