WPF 记一个Popup踩坑记录
看名字就知道,它是一个弹出控件,顾名思义,我们可以用它来实现类似Combobox那种,点击后弹出下面选项列表的操作。
记录:
需求:有一个文本框 ,鼠标点击后,弹出一个Popup。
我编写了以下xaml
<Grid><TextBox PreviewMouseDown="text_PreviewMouseDown" x:Name="text" Text="666" BorderBrush="Red" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top"Width="100" FontSize="30" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/><Popup Placement="Bottom" PlacementTarget="{Binding ElementName=text}"IsOpen="{Binding IsOpen1}"AllowsTransparency="True" StaysOpen="False" Width="200" Height="200"><Border Margin="10" Background="Green"><TextBox Text="弹出内容1" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></Popup>
</Grid>
这时,奇怪的现象出现了,我鼠标点击textbox后,一松开,popup就消失了。。。
经过一番研究,发现。
问题出在textbox的点击事件上,在PreviewMouseDown事件执行完毕之后,焦点会移到textbox上,这里popup就失去焦点了。。
编写以下MainWindow.xaml:
<Window x:Class="wpfcore.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:wpfcore"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid><TextBox PreviewMouseUp="text_PreviewMouseDown" x:Name="text" Text="666" BorderBrush="Red" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top"Width="100" FontSize="30" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/><Popup Placement="Bottom" PlacementTarget="{Binding ElementName=text}"IsOpen="{Binding IsOpen1}"AllowsTransparency="True" StaysOpen="False" Width="200" Height="200"><Border Margin="10" Background="Green"><TextBox Text="弹出内容1" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></Popup></Grid><Grid Grid.Column="1"><TextBlock MouseUp="textBlock_PreviewMouseDown" x:Name="textblock" Text="666" Background="LightBlue" HorizontalAlignment="Left" VerticalAlignment="Top"Width="100" FontSize="30" TextAlignment="Center"/><Popup Placement="Bottom" PlacementTarget="{Binding ElementName=textblock}"IsOpen="{Binding IsOpen2}"AllowsTransparency="True" StaysOpen="False" Width="200" Height="200"><Border Margin="10" Background="Green"><TextBox Text="弹出内容2" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></Popup></Grid></Grid>
</Window>
MainWindow.cs代码如下:
using System.Windows;
using System.Windows.Input;namespace wpfcore
{public partial class MainWindow : Window{public bool IsOpen1{get { return (bool)GetValue(IsOpen1Property); }set { SetValue(IsOpen1Property, value); }}public static readonly DependencyProperty IsOpen1Property =DependencyProperty.Register("IsOpen1", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));public bool IsOpen2{get { return (bool)GetValue(IsOpen2Property); }set { SetValue(IsOpen2Property, value); }}public static readonly DependencyProperty IsOpen2Property =DependencyProperty.Register("IsOpen2", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));public MainWindow(){InitializeComponent();DataContext = this;}private void text_PreviewMouseDown(object sender, MouseButtonEventArgs e){IsOpen1 = true;}private void textBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e){IsOpen2 = true;}}
}
通过对比可以发现,使用一个TextBlock的效果比textbox好多了。。
以下是对比效果:
记录下来,备查。
WPF 记一个Popup踩坑记录相关推荐
- 记一次编译Cesium踩坑记录
记一次编译Cesium踩坑记录 cd cesium npm install npm install express --save npm install compression --save npm ...
- 【踩坑记录】mybatis-plus的insert方法,默认会生成一个uuid作为主键,导致类型不一致,存入数据库报错
[踩坑记录]mybatis-plus的insert方法,默认会生成一个uuid作为主键,导致类型不一致,存入数据库报错 报错记录 解决方案 推荐方案 使用uuid作为主键,修改id的类型为bigint ...
- 日常踩坑记录-汇总版
开发踩坑记录,不定时更新 心得 RTFM 严谨的去思考问题,处理问题 严格要求自己的代码编写习惯与风格 注意 单词拼写 20200207 mybatis plus 自带insert插入异常 sql i ...
- linux命令行下载cuda,linux 命令行下安装特定版本的 cuda (踩坑记录)
其实可直接看第 4 步的总结...... 实验室服务器 Ubuntu 16.04 系统下原本安装的 cuda 版本是 9.0,硬件条件是一张 1080 Ti 的 GPU,在使用 PyTorch 的过程 ...
- 双系统Ubuntu22.04深度学习环境配置与踩坑记录
双系统Ubuntu22.04深度学习环境配置踩坑记录 前言 目录 相关版本 主要参考教程 Ubuntu安装 Nvidia和CUDA安装 踩坑经历 官网安装所遇问题 cuDNN安装 Anaconda安装 ...
- 树莓派3B+踩坑记录:一、安装Ubuntu Mate
树莓派3B+踩坑记录:一.安装Ubuntu Mate 2020-07-27 05:44:15 来源:互联网 分类:相关文章 树莓派3B+踩坑记录:一.安装Ubuntu Mate 树莓派,Ubuntu, ...
- Java 线上惨痛踩坑记录,你也一定遇到过
线上问题年年有,今年特别多.记几次线上惨痛的踩坑记录,希望大家以史为鉴. 1. 包装类型自动解箱导致空指针异常 public int getId() { Integer id = null; retu ...
- Java线上惨痛踩坑记录,你也一定遇到过
线上问题年年有,今年特别多.记几次线上惨痛的踩坑记录,希望大家以史为鉴. 1. 包装类型自动解箱导致空指针异常 public int getId() {Integer id = null;return ...
- 为Jupyter notebook配置R kernel过程及踩坑记录
为Jupyter notebook配置R kernel过程及踩坑记录 注意:本文为作者安装过程及折腾的过程,内容比较冗杂,如果读者想直接创建一个属于自己的子环境则参考: 如下文章: anaconda下 ...
最新文章
- Spring mvc-kaptcha 验证码
- 上传图片-服务端-Api接口定义
- 超微服务器双路主板系列,巨无霸核心!超微发布X12DPL系列服务器主板
- 今天发现新大陆:haml和Emmet
- 【快写】基本思路及模板
- android返回上一级代码,Android实践11 | 利用intent返回数据给上一级activity
- Hadoop完全分布式安装Kafka
- python调用pipe_Python中使用PIPE操作Linux管道
- mac install wget
- 第五章:Redis pipeline流水线
- 2016重庆大学计算机学院复试分数线,重庆大学2016考研复试分数线(已公布)
- next_permutation()
- php bt种子转换电驴地址,bt转换ed2k_BT文件转磁力链接工具 BT种子文件转换成ed2k链接...
- 智慧树工业机器人测试答案_知到智慧树玩转工业机器人答案章节单元测试答案...
- 存储过程,函数——mysql批量添加随机生成用户信息(生成随机数)
- Excel 有哪些可能需要熟练掌握而很多人不会的技能
- RocketMQ存储篇——整体结构以及MappedFile讲解
- js 获取两个数组的交集,并集,补集,差集(转载+收藏)
- httpd服务及配置文件详解
- HTML进阶--如何使用Sublime Text来创建网页 -------16岁的小前端
热门文章
- html的 button点击事件无效,InfoWindow里面加button,监听button点击事件无效 求解啊...
- java contains 通配符_java删除文件支持通配符
- 单片机按键防抖程序_这些单片机按键设计方案,请拿好,不谢!
- IIS7中使用集成模式时出现HttpException
- VMware——安装CentOS
- UI自动化web端框架path.py代码
- C#中二进制和流之间的各种相互转换
- poj2442Sequence(优先队列)
- 命令行下的mysql的基本操作
- 将Reporting Service 2005 SP2集成部署到WSS3或MOSS2007服务器场心得