zoukankan      html  css  js  c++  java
  • 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();
                    }
                }
                
            }
  • 相关阅读:
    Android如何使用注解进行代码检查
    Gradle系列之从零搭建Maven私服库
    Gradle系列之Android Gradle高级配置
    Gradle系列之Android Gradle基础配置
    Gradle系列之Android Gradle插件
    Gradle系列之Java Gradle插件
    一个年轻的女孩
    互联网不等于科技,互联网繁荣之下,毁掉的是工业科技发展
    《Kafka笔记》2、环境搭建、Topic管理
    《Kafka笔记》1、Kafka初识
  • 原文地址:https://www.cnblogs.com/dubuyunjie/p/3188525.html
Copyright © 2011-2022 走看看