Popup中ListBox的SelectChange事件关闭弹出窗体后主窗体点击无效BUG

WPF的BUG!
弹出框的 自定义控件里有Popup, Popup里面放一个ListBox

 在ListBox中的SelectionChange事件触发关闭弹出框后,主窗体存在一定概率卡死(但点击标题又能用的BUG)

 

步骤一: 新建个自定义WPF控件UserControl

Xaml代码:


    
        
            
                
                    >
                
                
            
        
        
            
                
                    
                        
                    
                
            
        
    

逻辑代码:

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            List> tupes = new List>();
            Enumerable.Range(1, 10).Select(p => p.ToString().PadLeft(3, '0')).ToList().ForEach(p => tupes.Add(new Tuple(p, p, p)));
            listboxSuggestion.ItemsSource = tupes;
            listboxSuggestion.SelectionChanged += (o1, e1) =>
            {
                //this.IsDropDownOpen = false;      //不能解决问题
                RoutedEventArgs args = new RoutedEventArgs(EnterDownEvent, o1); //选中项改变触发
                this.RaiseEvent(args);
            };            
        }

        #region 回车触发事件
        //声明和注册路由事件
        public static readonly RoutedEvent EnterDownEvent =
            EventManager.RegisterRoutedEvent(
                "EnterDown",
                RoutingStrategy.Bubble,
                typeof(EventHandler),
                typeof(UserControl1));
        //CLR事件包装
        public event RoutedEventHandler EnterDown
        {
            add { this.AddHandler(EnterDownEvent, value); }
            remove { this.RemoveHandler(EnterDownEvent, value); }
        }
        #endregion

        #region 是否打开下拉框
        public bool IsDropDownOpen
        {
            get { return (bool)GetValue(IsDropDownOpenProperty); }
            set
            {

                SetValue(IsDropDownOpenProperty, value);

            }
        }
        public static readonly DependencyProperty IsDropDownOpenProperty =
            DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(UserControl1), new PropertyMetadata(false));
        #endregion
    }

  

步骤二: 新建个Window窗体DialogWin

xaml代码

"WpfApplication1.DialogWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="DialogWin" Height="88.846" Width="210">
    
        "120" Height="22" x:Name="mySelect"  />
    

cs代码

public partial class DialogWin : Window
    {
        public DialogWin()
        {
            InitializeComponent();
            mySelect.EnterDown += (o1, e1) =>
            {
                //mySelect.IsDropDownOpen = false;              //不起作用
                //Dispatcher.InvokeAsync(Close);                //还是会卡顿
                this.Close();
            };            
        }
    }

  

步骤三,在主窗体弹出DialogWin

xaml代码


    
        
        
        
        

  cs代码

/// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var s = new DialogWin();
            s.Owner = this;
            s.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            s.ShowDialog();
        }
    }

运行程序....

Popup中ListBox的SelectChange事件关闭弹出窗体后主窗体点击无效BUG_第1张图片

解决方案: 开启线程延迟关闭弹出体【最无语做法】

代码下载

你可能感兴趣的:(Popup中ListBox的SelectChange事件关闭弹出窗体后主窗体点击无效BUG)