详细介绍:wpf之 Popup

news/2025/10/22 19:55:58/文章来源:https://www.cnblogs.com/ljbguanli/p/19158834

详细介绍:wpf之 Popup

在这里插入图片描述

前言

Popup 在WPF中用于创建浮动内容的控件,它显示在其他内容之上,常用于工具提示、弹出菜单等场景

1、IsOpen

指示是否显示控件,值为true显示控件,值为false不显示控件。

2、PlacementTarget

PlacementTarget 属性用于指定 Popup 相对于哪个控件进行定位,是控制 Popup 显示位置的关键属性,与 Placement 属性配合使用,下面的代码中指定Popup相对于btn1按钮定位,并且由于Placement="Left"所以是在btn1按钮的左边显示
在这里插入图片描述

<Window x:Class="wpf之Popup.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:wpf之Popup"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid><Grid.RowDefinitions><RowDefinition Height="1*" /><RowDefinition Height="1*" /></Grid.RowDefinitions ><Grid.ColumnDefinitions><ColumnDefinition Width=" *"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Button  x:Name="btn1"  Grid.Column=" 0" Grid.Row=" 0" Width="100"   Height=" 100" Background="Gray" Margin="3" Click="Button_Click"  HorizontalAlignment="Center" /><Button  x:Name="btn2"   Grid.Column=" 1" Grid.Row=" 1"   Width="100"   Height=" 100" Background="Gray" Click="Button_Click_1"   HorizontalAlignment="Center"/><Popup x:Name="pop_test1"  IsOpen="false" PlacementTarget="{Binding ElementName=btn1}" Placement="Left" StaysOpen="True"       AllowsTransparency="False"   PopupAnimation="None"  FlowDirection="LeftToRight" ><Border Background="#80FF0000" CornerRadius="10" Width="100" Height="100"><StackPanel Grid.Row=" 0" Orientation="Horizontal"  ><Button Width=" 50" Height=" 50" Background="Red"   /><Button Width=" 50" Height=" 50"  Background="Blue"   /></StackPanel ></Border></Popup ></Grid></Window>

3、Placement

指示Popup相对于PlacementTarget指定的控件的放置位置。
1)Absolute
相对于屏幕左上角定位
在这里插入图片描述
下面的代码指定Popup在距离屏幕左上角水平方向100(HorizontalOffset=“100”) ,垂直方向200(VerticalOffset=“200”)的位置显示。

<Window x:Class="wpf之Popup.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:wpf之Popup"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid><Grid.RowDefinitions><RowDefinition Height="1*" /><RowDefinition Height="1*" /></Grid.RowDefinitions ><Grid.ColumnDefinitions><ColumnDefinition Width=" *"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Button  x:Name="btn1"  Grid.Column=" 0" Grid.Row=" 0" Width="100"   Height=" 100" Background="Gray" Margin="3" Click="Button_Click"  HorizontalAlignment="Center" /><Button  x:Name="btn2"   Grid.Column=" 1" Grid.Row=" 1"   Width="100"   Height=" 100" Background="Gray" Click="Button_Click_1"   HorizontalAlignment="Center"/><Popup x:Name="pop_test1"  IsOpen="false"  Placement="Absolute"   HorizontalOffset="100" VerticalOffset="200" StaysOpen="True"       AllowsTransparency="False"   PopupAnimation="None"  FlowDirection="LeftToRight" ><Border Background="#80FF0000" CornerRadius="10" Width="100" Height="100"><StackPanel Grid.Row=" 0" Orientation="Horizontal"  ><Button Width=" 50" Height=" 50" Background="Red"   /><Button Width=" 50" Height=" 50"  Background="Blue"   /></StackPanel ></Border></Popup ></Grid></Window>

2)Relative
相对于指定的矩形左上角定位,矩形的坐标的原点在窗体的左上角
在这里插入图片描述
下面的代码指定popup在矩形(PlacementRectangle=“100,200,1000,1000” )指定的距离窗体左边100,上边200的位置显示。

<Window x:Class="wpf之Popup.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:wpf之Popup"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid><Grid.RowDefinitions><RowDefinition Height="1*" /><RowDefinition Height="1*" /></Grid.RowDefinitions ><Grid.ColumnDefinitions><ColumnDefinition Width=" *"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Button  x:Name="btn1"  Grid.Column=" 0" Grid.Row=" 0" Width="100"   Height=" 100" Background="Gray" Margin="3" Click="Button_Click"  HorizontalAlignment="Center" /><Button  x:Name="btn2"   Grid.Column=" 1" Grid.Row=" 1"   Width="100"   Height=" 100" Background="Gray" Click="Button_Click_1"   HorizontalAlignment="Center"/><Popup x:Name="pop_test1"  IsOpen="false"  Placement="Relative"   PlacementRectangle="100,200,1000,1000" StaysOpen="True"       AllowsTransparency="False"   PopupAnimation="None"  FlowDirection="LeftToRight" ><Border Background="#80FF0000" CornerRadius="10" Width="100" Height="100"><StackPanel Grid.Row=" 0" Orientation="Horizontal"  ><Button Width=" 50" Height=" 50" Background="Red"   /><Button Width=" 50" Height=" 50"  Background="Blue"   /></StackPanel ></Border></Popup ></Grid></Window>

3)Bottom
结合PlacementTarget使用,靠下显示
在这里插入图片描述

4)Top
结合PlacementTarget使用,靠上显示
在这里插入图片描述

5)Left
结合PlacementTarget使用,靠左显示
在这里插入图片描述

<Popup x:Name="pop_test1"  IsOpen="false"  PlacementTarget="{Binding ElementName=btn1}" Placement="Left" StaysOpen="True"       AllowsTransparency="False"   PopupAnimation="None"  FlowDirection="LeftToRight" ><Border Background="#80FF0000" CornerRadius="10" Width="100" Height="100"><StackPanel Grid.Row=" 0" Orientation="Horizontal"  ><Button Width=" 50" Height=" 50" Background="Red"   /><Button Width=" 50" Height=" 50"  Background="Blue"   /></StackPanel ></Border></Popup >

6)Right
结合PlacementTarget使用,靠右显示
在这里插入图片描述

7)Center
结合PlacementTarget使用,居中显示
在这里插入图片描述

8)Mouse
根据鼠标位置决定,不需要结合PlacementTarget使用

9)MousePoint
根据鼠标点击位置决定,不需要结合PlacementTarget使用

<Popup x:Name="pop_test1"  IsOpen="false"  Placement="MousePoint"        StaysOpen="True"       AllowsTransparency="False"   PopupAnimation="None"  FlowDirection="LeftToRight" ><Border Background="#80FF0000" CornerRadius="10" Width="100" Height="100"><StackPanel Grid.Row=" 0" Orientation="Horizontal"  ><Button Width=" 50" Height=" 50" Background="Red"   /><Button Width=" 50" Height=" 50"  Background="Blue"   /></StackPanel ></Border></Popup >

10)AbsolutePoint

4、StaysOpen

StaysOpen 决定了 Popup 是"临时性"的还是"持久性"的显示。
1)true
则为永久性显示,需要通过代码关闭Popup控件。
2) false
则为临时显示,当点击 不是Popup控件所显示的区域时,Popup控件不再显示。

5、AllowsTransparency

指示Popup控件是否支持透明色
1)true
支持透明色
2)false
不支持透明色

6、PopupAnimation

指示Popup控件显示时的动画,比如滑动显示

7、FlowDirection

指示Popup控件中的子控件的排列方式,比如从左到右还是从右到左。

综合应用

在这里插入图片描述

<Window x:Class="wpf之Popup.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:wpf之Popup"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid><Grid.RowDefinitions><RowDefinition Height="1*" /><RowDefinition Height="1*" /></Grid.RowDefinitions ><Grid.ColumnDefinitions><ColumnDefinition Width=" *"/><ColumnDefinition Width="*"/></Grid.ColumnDefinitions><Button  x:Name="btn1"  Grid.Column=" 0" Grid.Row=" 0" Width="100"   Height=" 100" Background="Gray" Margin="3" Click="Button_Click"  HorizontalAlignment="Center" /><Button  x:Name="btn2"   Grid.Column=" 1" Grid.Row=" 1"   Width="100"   Height=" 100" Background="Gray" Click="Button_Click_1"   HorizontalAlignment="Center"/><Popup x:Name="pop_test1"  IsOpen="false" PlacementTarget="{Binding ElementName=btn1}" Placement="Left" StaysOpen="False"      AllowsTransparency="False"   PopupAnimation="None"  FlowDirection="LeftToRight" ><Border Background="#80FF0000" CornerRadius="10" Width="100" Height="100"><StackPanel Grid.Row=" 0" Orientation="Horizontal"  ><Button Width=" 50" Height=" 50" Background="Red"   /><Button Width=" 50" Height=" 50"  Background="Blue"   /></StackPanel ></Border></Popup ><Popup x:Name="pop_test2"  IsOpen="false" PlacementTarget="{Binding ElementName=btn1}" Placement="Right"   HorizontalOffset=" 100" VerticalOffset=" 50" StaysOpen="True" AllowsTransparency="True"  PopupAnimation="Slide "    FlowDirection="RightToLeft" ><Border Background="#80FF0000" CornerRadius="10" Width="100" Height="100"><StackPanel Grid.Row=" 0" Orientation="Horizontal"  ><Button Width=" 50" Height=" 50" Background="Red"   /><Button Width=" 50" Height=" 50" Content="关闭" Background="Blue" Click="Button_Click_2"   /></StackPanel ></Border></Popup ></Grid></Window>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/943686.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

? #4

100 + 60 + 50 + 100 = 310, Rank 1/6.怎么 swap(min(B,C),D) /wx2024暑期CSP-S&NOIP模拟赛第2套 链接:link 题解:link 的题解区 时间:4h (2025.10.22 14:00~18:00) 题目数:4 难度:A B C D\(\color{#F39C11} …

结对项目-生成四则运算

这个作业属于哪个课程 https://edu.cnblogs.com/campus/gdgy/Class34Grade23ComputerScience/这个作业要求在哪里 https://edu.cnblogs.com/campus/gdgy/Class34Grade23ComputerScience/homework/13479姓名 学号 GitHu…

CSS3 超实用属性:pointer-events (可穿透图层的鼠标事件)

🧑‍💻 写在开头 点赞 + 收藏 === 学会🤣🤣🤣 CSS3 pointer-events 属性:实现可穿透图层的鼠标事件 在网页开发中,我们通常会遇到多个元素重叠的情况。在这种情况下,如何使得被遮挡的元素仍然能够响应鼠标…

C++开源库使用:nlohmann/json - 指南

C++开源库使用:nlohmann/json - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Mona…

实用指南:JAVA学习-预科部分(路线、博客、预备基础)

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

【比赛记录】2025CSP+NOIP 冲刺模拟赛合集Ⅱ

2025CSP-S模拟赛65(HZOJ CSP-S模拟37)A B C D Sum Rank100 40 15 - 155 7/12HZOJ 上也有这场比赛,但我没看见。放过去大概是个 14/24 左右吧。 A. gcd&xor (gcdxor) 首先打表,发现对于所有合法的 \((x,y)\),都…

取证-windbg和dmp,以及文件分析基本流程

.dmp文件及Dump Flie,是一种内存快照文件 说到内存快照就不得不提一下文件类型 说明 常见用途.raw 原始磁盘映像文件(Raw Image),完整保存磁盘或内存的原始二进制数据。 虚拟机快照、数字取证、系统备份。.dmp 内存…

20232422 2025-2026-1 《网络与系统攻防技术》实验二实验报告

后门原理与实践 1.1实验内容 这次的实验主要练了几种获取主机操作权限和收集信息的方法。先是用netcat配合Linux的cron定时任务,还有socat搭配系统任务计划,分别搞到了主机的操作Shell,拿到了控制主机的入口。然后用…

羊驼二次免疫的六大风险:纳米抗体制备不可忽视的 “隐形陷阱”

随着纳米抗体在肿瘤治疗、病原体检测、工业酶固定化等领域的应用拓展,对羊驼免疫及 VHH 筛选的需求持续攀升。羊驼因饲养、运输、免疫成本显著高于小鼠、兔子,市场上逐渐出现 “二次免疫” 操作 —— 即利用已免疫过…

完整教程:C++项目:仿muduo库高并发服务器-------connection模块

完整教程:C++项目:仿muduo库高并发服务器-------connection模块pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "…

营销数字化专家要求

目录背景和价值高级营销数字化专家(整合营销)职位描述任职要求参考资料 背景和价值高级营销数字化专家(整合营销) 深圳市 | 产品及解决方案类 职位描述营销数字化转型规划:对整合营销端到端流程,KOL营销、社媒营…

深入解析:线性代数 SVD | 令人困扰的精度 1

深入解析:线性代数 SVD | 令人困扰的精度 1pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &qu…

小程序反编译包的架构文件

common是逻辑的代码 pages 存放小程序的页面,路径等 app.js 也是小程序的脚本代码 app.json 配置文件

【最终章】-串口收发指令处理器-Verilog语法学习EP12 - 教程

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

10.22 CSP-S模拟37/2025多校冲刺CSP模拟赛7 改题记录

10.22HZOJ 写在前面 ACCODERS+洛谷双重大凶然后又加了场模拟赛。怎么感觉每次大凶就会临时加模拟赛。。。然后就是连续第inf场模拟赛切不了T1。。。疑似失去了所有的力气与手段。然后T2以为是假做法拿了25pts沾沾自喜,…

完整教程:LeapMotion_Demo演示

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

[题解]P11126 [ROIR 2024] 三等分的数组 (Day 2)

P11126 [ROIR 2024] 三等分的数组 (Day 2) 考虑到数的选取与输入顺序无关,我们将数丢到桶里,记 \(c_x\) 为 \(x\) 出现的次数。 那么我们取出三元组的过程可以描述为下面二者之一:选取 \(c\) 中的一个位置,将其减去…

Acrobat Pro DC 2025下载及破解安装教程,附永久免费免激活中文版Acrobat Pro DC安装包(稳定版)

一、Acrobat Pro DC 2025软件下载[软件名称]: Acrobat Pro DC 2025(稳定版)[软件大小]: 1.63GB[安装环境]:Win 10及以上系统[下载链接]: (建议手机保存后到电脑端打开,下载解压无需任何密码)夸克:https://pan.qua…

VSLAM 十四讲--阅读中知识点记录

1. 前言砚上三五笔,落墨鹧鸪啼本文用于记录:VSLAM相关。 PS:笔者梦到哪里写哪里,毫无逻辑可言。。。 如有不对,欢迎评论区指正! 2. 正文 2.1 slamsimultaneous location and mapping 同步定位和建图相机:单目相…

20232307 2025-2026-1 《网络与系统攻防技术》实验二实验报告

20232307 2025-2026-1 《网络与系统攻防技术》实验二实验报告 1. 实验内容 相关知识:后门就是不经过正常认证流程而访问系统的通道。后门类型:编译器留后门、操作系统留后门、应用程序中留后门、还有潜伏于操作系统中…