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();
                }
            }
            
        }
原文地址:https://www.cnblogs.com/dubuyunjie/p/3188525.html