• WPF popup自动关闭


              var tileMore = new Tile
                        {
                            Height = 40,
                            Width = 85,
                            Background = new SolidColorBrush(Color.FromRgb(120, 240, 120)),
                            Title = "更多...",
                        };
                    tileMore.SetResourceReference(FrameworkElement.StyleProperty, "KentTile");
    
                    Popup popup = new Popup
                        {
                            //StaysOpen = false,
                            PopupAnimation = PopupAnimation.Slide,
                            PlacementTarget = tileMore,
                            Placement = PlacementMode.Bottom,
                            AllowsTransparency = true
                        };
                    //pop里面生成的内容,本例是StackPannel中包括一个textbox
                    WrapPanel stackPanel = new WrapPanel
                        {
                            Width = tileMore.Width * 5,
                            Background = Brushes.Transparent
                        };
                    stackPanel.Children.Add(/*sth to show*/);
                    popup.Child = stackPanel;
    
                    tileMore.MouseEnter += (sender, e) =>
                        {
                            popup.IsOpen = true;
                        };
    
                    tileMore.MouseLeave += (s, e) =>
                    {
                        if (timerCloseRecentPopup == null)
                        {
                            timerCloseRecentPopup = new DispatcherTimer();
                            timerCloseRecentPopup.Interval = new TimeSpan(0, 0, 1);
                            timerCloseRecentPopup.Tag = popup;
                            timerCloseRecentPopup.Tick += closePopup;
                        }
                        timerCloseRecentPopup.Stop();
                        timerCloseRecentPopup.Start();
                    };
    
                    popup.MouseLeave += (s, e) =>
                        {
                            if(timerCloseRecentPopup == null)
                            {
                                timerCloseRecentPopup = new DispatcherTimer();
                                timerCloseRecentPopup.Interval = new TimeSpan(0, 0, 1);
                                timerCloseRecentPopup.Tag = popup;
                                timerCloseRecentPopup.Tick += closePopup;
                            }
                            timerCloseRecentPopup.Stop();
                            timerCloseRecentPopup.Start();
                        };

     

         /// <summary>
            /// 定时关闭 更多 popup窗口
            /// </summary>
            private DispatcherTimer timerCloseRecentPopup;
            private void closePopup(object state,EventArgs e)
            {
                Popup pop = timerCloseRecentPopup.Tag as Popup;
                if(pop == null)
                {
                    //todo timer里面的Assert没有对话框出来
                    Debug.Assert(true,"pop==null");
                    return;
                }
    
                Tile tileMore = pop.PlacementTarget as Tile;
    
                if (!pop.IsMouseOver )
                {
                    if(tileMore != null)
                    {
                        if(!tileMore.IsMouseOver)
                        {
                            pop.IsOpen = false;
                            timerCloseRecentPopup.Stop();
                        }
    
                    }
                    else
                    {
                        pop.IsOpen = false;
                        timerCloseRecentPopup.Stop();
                    }
                }
                
            }
  • 相关阅读:
    C# 中的委托和事件
    sql笔记-group by 统计功能
    js,css小知识点记录
    sql小技巧
    《孙子兵法》总结
    .Net深复制、浅复制
    《君主论》
    php邮箱找回密码功能
    后台管理员账号不能同时登陆,以及登陆使对方强制下线功能
    好程序员应该读的30本书
  • 原文地址:https://www.cnblogs.com/dubuyunjie/p/3188525.html
Copyright © 2020-2023  润新知