• 弹出式控件Popup


    效果图:

    点击显示按钮,显示Popup控件

     xam代码:

    <ctrl:uiPopup x:Name="canvas" Width="150" Grid.Column="1" Placement="Right" Height="{Binding ElementName=window,Path=Height}"
                          VerticalOffset="0" HorizontalOffset="0" IsOpen="False" AllowsTransparency="True" PopupAnimation="Fade">
                    <Border CornerRadius="0,5,5,0" Background="White" Margin="5">
                        <Border.Effect>
                            <DropShadowEffect Color="Gray" ShadowDepth="0" BlurRadius="5" Opacity="0.3" Direction="0"/>
                        </Border.Effect>
                        <Grid Background="White">
                            <Label Content="111111" FontSize="20"/>
                        </Grid>
                    </Border>
                </ctrl:uiPopup>

    重写Popup控件:

    public class uiPopup : Popup
        {
            /// <summary>  
            /// 是否窗口随动,默认为随动(true)  
            /// </summary>  
            public bool IsPositionUpdate
            {
                get { return (bool)GetValue(IsPositionUpdateProperty); }
                set { SetValue(IsPositionUpdateProperty, value); }
            }
    
            public static readonly DependencyProperty IsPositionUpdateProperty =
                DependencyProperty.Register("IsPositionUpdate", typeof(bool), typeof(uiPopup), new PropertyMetadata(true, new PropertyChangedCallback(IsPositionUpdateChanged)));
    
            private static void IsPositionUpdateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                (d as uiPopup).pup_Loaded(d as uiPopup, null);
            }
    
            /// <summary>  
            /// 加载窗口随动事件  
            /// </summary>  
            public uiPopup()
            {
                this.Loaded += pup_Loaded;
            }
    
            /// <summary>  
            /// 加载窗口随动事件  
            /// </summary>  
            private void pup_Loaded(object sender, RoutedEventArgs e)
            {
                Popup pup = sender as Popup;
                var win = VisualTreeHelper.GetParent(pup);
                while (win != null && (win as Window) == null)
                {
                    win = VisualTreeHelper.GetParent(win);
                }
                if ((win as Window) != null)
                {
                    (win as Window).LocationChanged -= PositionChanged;
                    (win as Window).SizeChanged -= PositionChanged;
                    if (IsPositionUpdate)
                    {
                        (win as Window).LocationChanged += PositionChanged;
                        (win as Window).SizeChanged += PositionChanged;
                    }
                }
            }
    
            /// <summary>  
            /// 刷新位置  
            /// </summary>  
            private void PositionChanged(object sender, EventArgs e)
            {
                try
                {
                    var method = typeof(Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                    if (this.IsOpen)
                    {
                        method.Invoke(this, null);
                    }
                }
                catch
                {
                    return;
                }
            }
    
            //是否最前默认为非最前(false)  
            public static DependencyProperty TopmostProperty = Window.TopmostProperty.AddOwner(typeof(Popup), 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 uiPopup).UpdateWindow();
            }
    
            /// <summary>  
            /// 重写拉开方法,置于非最前  
            /// </summary>  
            /// <param name="e"></param>  
            protected override void OnOpened(EventArgs e)
            {
                UpdateWindow();
            }
    
            /// <summary>  
            /// 刷新Popup层级  
            /// </summary>  
            private void UpdateWindow()
            {
                var hwnd = ((HwndSource)PresentationSource.FromVisual(this.Child)).Handle;
                RECT rect;
                if (NativeMethods.GetWindowRect(hwnd, out rect))
                {
                    NativeMethods.SetWindowPos(hwnd, Topmost ? -1 : -2, rect.Left, rect.Top, (int)this.Width, (int)this.Height, 0);
                }
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int Left;
                public int Top;
                public int Right;
                public int Bottom;
            }
            #region P/Invoke imports & definitions  
            public static class NativeMethods
            {
                [DllImport("user32.dll")]
                [return: MarshalAs(UnmanagedType.Bool)]
                internal static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
                [DllImport("user32", EntryPoint = "SetWindowPos")]
                internal static extern int SetWindowPos(IntPtr hWnd, int hwndInsertAfter, int x, int y, int cx, int cy, int wFlags);
            }
            #endregion
        }
    uiPopup来自网络。
  • 相关阅读:
    艾伟_转载:学习 ASP.NET MVC (第三回)实战篇 狼人:
    艾伟_转载:40条ASP.NET开发Tip 狼人:
    艾伟_转载:20条.NET编码习惯 狼人:
    艾伟_转载:数组排序方法的性能比较(上):注意事项及试验 狼人:
    艾伟_转载:使用LINQ to SQL更新数据库(上):问题重重 狼人:
    艾伟_转载:学习 ASP.NET MVC (第四回)实战篇 狼人:
    艾伟_转载:学习 ASP.NET MVC (第五回)理论篇 狼人:
    艾伟_转载:ASP.NET MVC 2博客系列 狼人:
    艾伟_转载:Cookie是什么?用法是怎样?与SESSION有什么区别?(二) 狼人:
    艾伟_转载:ASP.NET MVC 2博客系列之一:强类型HTML辅助方法 狼人:
  • 原文地址:https://www.cnblogs.com/Serialport/p/16104432.html
Copyright © 2020-2023  润新知