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

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

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

Xaml代码:

<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="30" d:DesignWidth="200">
<Grid>
<Grid x:Name="PART_Container">
<DockPanel Margin="0,0,1,0">
<ToggleButton x:Name="PART_ToggBtn" DockPanel.Dock="Right" BorderThickness="1" BorderBrush="#959595" Margin="-1,0,0,0"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:UserControl1}}">
>
</ToggleButton>
<TextBox x:Name="txtAutoComplete" />
</DockPanel>
</Grid>
<Popup x:Name="PART_Popup" Opacity="0" Width="{Binding ElementName=PART_Container,Path=ActualWidth}"
IsOpen="{Binding Path=IsDropDownOpen,Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:UserControl1}}"
PopupAnimation="Slide" Placement="Bottom" StaysOpen="False" AllowsTransparency="True"
PlacementTarget="{Binding ElementName=txtAutoComplete}" VerticalOffset="0" MinHeight="50" MaxHeight="300">
<ListBox x:Name="listboxSuggestion" BorderBrush="Transparent" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Item1}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Popup>
</Grid>
</UserControl>

逻辑代码:

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
List<Tuple<string, string, string>> tupes = new List<Tuple<string, string, string>>();
Enumerable.Range(1, 10).Select(p => p.ToString().PadLeft(3, '0')).ToList().ForEach(p => tupes.Add(new Tuple<string, string, string>(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<RoutedEventArgs>),
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代码

<Window x:Class="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">
<Grid>
<local:UserControl1 Width="120" Height="22" x:Name="mySelect" />
</Grid>
</Window>

cs代码

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

  

步骤三,在主窗体弹出DialogWin

xaml代码

<Window x:Class="WpfApplication1.MainWindow"
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="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="44,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="198,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="350,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="44,82,0,0"/>
<Button x:Name="button1" Content="弹出对话框" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="156,82,0,0" Click="button1_Click"/> </Grid>
</Window>

  cs代码

/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
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. fancybox 关闭弹出窗口 parent.$.fancybox.close(); 无反应 fancybox 关闭弹出窗口父页面自动刷新,弹出子窗口前后事件

    当我们在父页面使用 fancybox 弹出窗口后,如果想自己手动关闭,则可以 function Cancel() { parent.$.fancybox.close(); } 如果关闭没有反应,最好看 ...

  2. [转]js中confirm实现执行操作前弹出确认框的方法

    原文地址:http://www.jb51.net/article/56986.htm 本文实例讲述了js中confirm实现执行操作前弹出确认框的方法.分享给大家供大家参考.具体实现方法如下: 现在在 ...

  3. [Flex] PopUpButton系列 —— 打开和关闭弹出菜单

    <?xml version="1.0" encoding="utf-8"?><!--响应打开和关闭弹出菜单的例子 PopUpButtonOpe ...

  4. layui关闭弹出层

    layui关闭弹出层,今天我在vscode中使用p parent.layer.closeAll()发现没效果 换成layer.closeAll()就解决了这个问题. 由此我觉得关闭layui关闭弹出层 ...

  5. layer关闭弹出层,弹出打印

    常规的话,下面能够完成关闭弹出层 var index = parent.layer.getFrameIndex(window.name); //延迟关闭 解决打印窗口弹不出来的情况 parent.la ...

  6. layer实现关闭弹出层刷新父界面功能详解

    本文实例讲述了layer实现关闭弹出层刷新父界面功能.分享给大家供大家参考,具体如下: layer是一款近年来备受青睐的web弹层组件,她具备全方位的解决方案,致力于服务各水平段的开发人员,您的页面会 ...

  7. Anroid关于fragment控件设置长按事件无法弹出Popupwindows控件问题解决记录

    一.问题描述     记录一下最近在安卓的gragment控件中设置长按事件遇见的一个坑!!!     在正常的activity中整个活动中设置长按事件我通常实例化根部局,例如LinearLayout ...

  8. Layui关闭弹出层对话框--刷新父界面

    在毕设的开发中,添加用户.添加权限等等一些地方需要类似于bootstrap中的模态框.然而开发用的却是layui 在layui中有弹出层可以实现其中的效果. 但是,一般用的时候都是提交后关闭窗口,刷新 ...

  9. 在Winform框架的多文档界面中实现双击子窗口单独弹出或拖出及拽回的处理

    在基于DevExpress的多文档窗口界面中,我们一般使用XtraTabbedMdiManager来管理多文档窗口的一些特性,如顶部菜单,页面的关闭按钮处理,以及一些特殊的设置,本篇随笔介绍这些特点, ...

随机推荐

  1. 2、tensorflow 变量的初始化

    https://blog.csdn.net/mzpmzk/article/details/78636137 关于张量tensor的介绍 import tensorflow as tf import n ...

  2. LeetCode初级算法之数组:122 买卖股票的最佳时机 II

    买卖股票的最佳时机 II 题目地址:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 给定一个数组,它的第 i ...

  3. jQuery插件的2种类型

    1.封装方法插件  封装方法插件在本质上来说,是一个对象级别的插件,这类插件首先通过jQuery选择器获取对象,并为对象添加方法,然后,将方法进行打包,封闭成一个插件,这种类型的插件编写简单,极易调用 ...

  4. springboot中使用h2数据库(内存模式)

    使用H2的优点,不需要装有服务端和客户端,在项目中包含一个jar即可,加上初始化的SQL就可以使用数据库了 在springboot中引入,我的版本是2.1.4,里面就包含有h2的版本控制 <!- ...

  5. STM32 GPIO输入输出(基于HAL库)

    一.基础认识 GPIO全名为General Purpose Input Output,即通用输入输出.有时候简称为"IO口".通用,说明它是常见的.输入输出,就是说既能当输入口使用 ...

  6. sqli-labs 54-65(CHALLANGES)

    challenges less-54 less-55 less-56 less-57 less-58 less-59 less-60 less-61 less-62 less-63 less-64 l ...

  7. 马赛克密码破解——GitHub 热点速览 Vol.50

    作者:HelloGitHub-小鱼干 "xx"(爆粗口) 这个词是最能体现本人看到本周 GitHub 热点的心情的.那一天,看到用图片处理技术还原马赛克密码的 Depix 便惊为天 ...

  8. 2. 使用Shell能做什么

    批处理 在批处理的过程中,能够实现脚步自动化,比GUI自动化速度高效 日常工作场景 服务端测试 移动端测试 持续集成与自动化部署,这是最最场景的场景,可以说离开了shell,持续集成和自动化部署也会遇 ...

  9. vue第十一单元(内置组件)

    第十一单元(内置组件) #课程目标 熟练掌握component组件的用法 熟练使用keep-alive组件 #知识点 #1.component组件 component是vue的一个内置组件,作用是:配 ...

  10. 解决uiautomator截取不到手机App界面信息

    今天在使用uiautomatorviewer进行安卓app控件定位的时候,出现以下异常,(用的是真机测试Android版本是10,据说是Android 8以后sdk自带的uiautomator直接打开 ...