WPF Popup 置顶问题

本文来自白锦文的博客,原文地址:http://blog.csdn.net/Baijinwen/archive/2011/01/22/6159043.aspx

(ps:经验证可用)

问题:

使用wpf的popup,当在popup中弹出MessageBox或者打开对话框的时候,popup总是置顶,并遮住MessageBox或对话框.

解决:

写如下用户控件

 

需导入的空间: using System.Windows.Controls.Primitives;

    using System.Runtime.InteropServices;

    using System.Windows.Interop;

 1 public class CCPopup : Popup
 2     {
 3         public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(typeof(CCPopup), new FrameworkPropertyMetadata(false, OnTopmostChanged));
 4         public bool Topmost
 5         {
 6             get { return (bool)GetValue(TopmostProperty); }
 7             set { SetValue(TopmostProperty, value); }
 8         }
 9         private static void OnTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
10         {
11             (obj as CCPopup).UpdateWindow();
12         }
13         protected override void OnOpened(EventArgs e)
14         {
15             UpdateWindow();
16         }
17         private void UpdateWindow()
18         {
19             var hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
20             RECT rect;
21             if (GetWindowRect(hwnd, out rect))
22             {
23                 SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0);
24             }
25         }
26         #region P/Invoke imports & definitions
27         [StructLayout(LayoutKind.Sequential)]
28         public struct RECT
29         {
30             public int Left;
31             public int Top;
32             public int Right;
33             public int Bottom;
34         }
35         [DllImport("user32.dll")]
36         [return: MarshalAs(UnmanagedType.Bool)]
37         private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
38         [DllImport("user32", EntryPoint = "SetWindowPos")]
39         private static extern int SetWindowPos(IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags);
40         #endregion
41     }

 

转载于:https://www.cnblogs.com/MarcLiu/p/3688134.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF中的Popup是一种特殊的控件,它可以显示在其他控件上方,并且可以在需要时弹出和隐藏。Popup的样式可以通过重写来自定义。 首先,在WPF中定义Popup的样式需要使用XAML语言。可以在Window或者Page的资源中定义样式,也可以在单独的资源文件中定义。 我们可以通过为Popup定义一个控制模板来重写其样式。控制模板是一种描述控件外观和行为的XAML标记。 以下是一个简单的示例,展示了如何重写Popup的样式: ```xaml <Style TargetType="Popup"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Popup"> <Grid Background="Transparent"> <!-- 在此处定义你想要的Popup的样式 --> <Border Background="White" BorderBrush="Gray" BorderThickness="1" CornerRadius="5"> <ContentPresenter /> </Border> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 在这个示例中,我们重写了Popup的样式,使其在显示时以圆角灰色边框包裹内容。可以根据需要自定义Border的属性,如背景色、边框颜色、边框粗细等。 当我们使用自定义样式的Popup控件时,只需要将该样式应用到需要的控件上即可: ```xaml <Button Content="点击弹出Popup"> <Button.Resources> <Style TargetType="Popup" BasedOn="{StaticResource {x:Type Popup}}"> <!-- 在这里进一步修改样式,如果有需要 --> </Style> </Button.Resources> <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <BeginStoryboard> <Storyboard> <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsOpen"> <DiscreteBooleanKeyFrame KeyTime="0" Value="True" /> </BooleanAnimationUsingKeyFrames> </Storyboard> </BeginStoryboard> </EventTrigger> </Button.Triggers> <Button.ContentTemplate> <DataTemplate> <Popup PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}}" Placement="Bottom"> <!-- 在这里放置Popup的内容 --> <TextBlock Text="这是一个Popup" Margin="5" /> </Popup> </DataTemplate> </Button.ContentTemplate> </Button> ``` 通过上述方式,我们可以重写WPF中的Popup样式,实现更加个性化的外观效果。同时,通过定义控制模板,我们还可以进一步自定义Popup的布局和动画效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值