How to fix when WPF Popup control is always topmost over other application windows?
The WPF Popup control is always "topmost" over other application windows. Please follow below steps to fix this issue.
Step 1- Create your own popup class & derive it from Popup class
In above code GetWindowRect is used to set current window location. TopmostProperty have bonded with event OnTopmostChanged which updates the window.
Step-2 Add namespace reference in your XAML Page.
Step-3 Call popup control
That's it your popup will works properly.
Step 1- Create your own popup class & derive it from Popup class
public class PopupNonTopmost : Popup { public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner( typeof(PopupNonTopmost), 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 PopupNonTopmost).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 }
In above code GetWindowRect is used to set current window location. TopmostProperty have bonded with event OnTopmostChanged which updates the window.
Step-2 Add namespace reference in your XAML Page.
xmlns:cc="clr-namespace:your_wpf_refrence"
Step-3 Call popup control
<cc:PopupNonTopmost Name="Popup1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="194" Height="200" IsOpen="True"> <TextBlock Name="McTextBlock" Background="LightBlue" > This is popup text </TextBlock> </cc:PopupNonTopmost>
0 comments :
Post a Comment