WPF 显示气泡提示框

气泡提示框应用举例

        有时候在我们开发的软件经常会遇到需要提示用户的地方,为了让用户更直观,快速了解提示信息,使用简洁、好看又方便的气泡提示框显得更加方便,更具人性化。如下面例子:(当用户未输入账号时,气泡提示会在登录页面上方提示用户“请输入登录账号”,用户点击其他地方或者在无操作一段时间后,气泡会自动消失。根据不同类型的提示框,字体颜色和图标也不一样。本例的气泡提示框可在不同窗体上显示。)

接下来就开始做一个有趣的气泡提示框吧(形状可自定义)

气泡提示框代码创建

新建气泡提示框窗体

        新建一个气泡提示框窗体(之所以选择窗体是因为可以让它不附属于其他窗体,可在任意窗体上运行),使用Popup控件作为提示框主体。在Popup控件中添加提示图标和提示信息,通过InfoType依赖属性来设置其图标和信息背景颜色。

<Window x:Class="BubblePrompt.PopupMessageWindow"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:BubblePrompt"mc:Ignorable="d"Name="own" AllowsTransparency="True" ShowInTaskbar="False"Title="PopupMessageWindow" Height="60" Width="410" WindowStyle="None"   Background="{x:Null}" ><Popup Name="popupInfo" Opened="popupInfo_Opened"  StaysOpen="False" IsOpen="False" AllowsTransparency="True" VerticalOffset="60" HorizontalOffset="100"  Placement="Top"  PlacementTarget="{Binding Element,ElementName=own}"><Border CornerRadius="4" Background="{Binding BackColor,ElementName=own}" Height="50" Width="400" ><Grid><Image Width="20" Height="20" Source="{Binding ImageSource,ElementName=own}" HorizontalAlignment="Left" Margin="30,0,0,0"/><TextBlock x:Name="txtbInfo" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="60,0,0,0" FontSize="16"Text="{Binding Info,ElementName=own}" Foreground="{Binding InfoTextColor,ElementName=own}"/></Grid></Border></Popup>
</Window>
    /// <summary>/// PopupMessageWindow.xaml 的交互逻辑/// </summary>public partial class PopupMessageWindow : Window{private static BitmapImage NormalInfoImg = new BitmapImage(new Uri("/BubblePrompt;component/Images/ic_pop_normal.png", UriKind.RelativeOrAbsolute));private static BitmapImage WarnInfoImg = new BitmapImage(new Uri("/BubblePrompt;component/Images/ic_pop_warn.png", UriKind.RelativeOrAbsolute));private static BitmapImage ErrorInfoImg = new BitmapImage(new Uri("/BubblePrompt;component/Images/ic_pop_fail.png", UriKind.RelativeOrAbsolute));private static Brush NormalBackColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#DEF4FF"));private static Brush WarnBackColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF0E6"));private static Brush ErrorBackColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFF0E6"));private static Brush NormalInfoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#0094FF"));private static Brush WarnInfoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FB9048"));private static Brush ErrorInfoColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FB9048"));System.Windows.Threading.DispatcherTimer uiTimer;public FrameworkElement Element{get { return (FrameworkElement)GetValue(ElementProperty); }set { SetValue(ElementProperty, value); }}// Using a DependencyProperty as the backing store for Element.  This enables animation, styling, binding, etc...public static readonly DependencyProperty ElementProperty =DependencyProperty.Register("Element", typeof(FrameworkElement), typeof(Window), new PropertyMetadata(null));public string Info{get { return (string)GetValue(InfoProperty); }set { SetValue(InfoProperty, value); }}// Using a DependencyProperty as the backing store for Info.  This enables animation, styling, binding, etc...public static readonly DependencyProperty InfoProperty =DependencyProperty.Register("Info", typeof(string), typeof(Window), new PropertyMetadata(""));public int Delay{get { return (int)GetValue(DelayProperty); }set { SetValue(DelayProperty, value); }}// Using a DependencyProperty as the backing store for Delay.  This enables animation, styling, binding, etc...public static readonly DependencyProperty DelayProperty =DependencyProperty.Register("Delay", typeof(int), typeof(Window), new PropertyMetadata(3));public Brush BackColor{get { return (Brush)GetValue(BackColorProperty); }set { SetValue(BackColorProperty, value); }}// Using a DependencyProperty as the backing store for BackColor.  This enables animation, styling, binding, etc...public static readonly DependencyProperty BackColorProperty =DependencyProperty.Register("BackColor", typeof(Brush), typeof(Window), new PropertyMetadata(NormalBackColor));public Brush InfoTextColor{get { return (Brush)GetValue(InfoTextColorProperty); }set { SetValue(InfoTextColorProperty, value); }}// Using a DependencyProperty as the backing store for BackColor.  This enables animation, styling, binding, etc...public static readonly DependencyProperty InfoTextColorProperty =DependencyProperty.Register("InfoTextColor", typeof(Brush), typeof(Window), new PropertyMetadata(NormalInfoColor));/// <summary>/// 0:Normal 1:Warn 2:Error/// </summary>public int InfoType{get { return (int)GetValue(InfoTypeProperty); }set{SetValue(InfoTypeProperty, value);if (value == 0){ImageSource = NormalInfoImg;BackColor = NormalBackColor;InfoTextColor = NormalInfoColor;}else if (value == 1){ImageSource = WarnInfoImg;BackColor = WarnBackColor;InfoTextColor = WarnInfoColor;}else if (value == 2){ImageSource = ErrorInfoImg;BackColor = ErrorBackColor;InfoTextColor = ErrorInfoColor;}}}// Using a DependencyProperty as the backing store for InfoType.  This enables animation, styling, binding, etc...public static readonly DependencyProperty InfoTypeProperty =DependencyProperty.Register("InfoType", typeof(int), typeof(Window), new PropertyMetadata(0));public BitmapImage ImageSource{get { return (BitmapImage)GetValue(ImageSourceProperty); }set { SetValue(ImageSourceProperty, value); }}// Using a DependencyProperty as the backing store for ImageSource.  This enables animation, styling, binding, etc...public static readonly DependencyProperty ImageSourceProperty =DependencyProperty.Register("ImageSource", typeof(BitmapImage), typeof(Window), new PropertyMetadata(null));DateTime startTime;public PopupMessageWindow(){InitializeComponent();}private void UITimerTick(object sender, EventArgs e){if (popupInfo.IsOpen == false){uiTimer.Stop();return;}if ((DateTime.Now - startTime).TotalSeconds >= Delay){this.Hide();popupInfo.IsOpen = false;}}private void popupInfo_Opened(object sender, EventArgs e){if (uiTimer == null || uiTimer.IsEnabled == false){uiTimer = new System.Windows.Threading.DispatcherTimer();uiTimer.Interval = TimeSpan.FromMilliseconds(1000);uiTimer.Tick += UITimerTick;startTime = DateTime.Now;uiTimer.Start();}else{startTime = DateTime.Now;}}}
新建气泡提示框管理类

        新建气泡提示框管理类作为公共类,使其可以被全局访问和使用。

    public class MessageManager{private PopupMessageWindow mPopupMessageWindow = new PopupMessageWindow();/// <summary>/// Poup消息提示/// </summary>public PopupMessageWindow PopupMessageWindow { set { mPopupMessageWindow = value; } get { return mPopupMessageWindow; } }/// <summary>/// type: 0:常规 1:注意 2:警告/// </summary>/// <param name="element"></param>/// <param name="type"></param>public void ShowMessageTip(string info, FrameworkElement element = null, int type = 0, int delaytime = 3){if (PopupMessageWindow != null && info.Length <= 30){PopupMessageWindow.Element = element;PopupMessageWindow.InfoType = type;PopupMessageWindow.popupInfo.IsOpen = true;PopupMessageWindow.Info = info;PopupMessageWindow.Delay = delaytime;PopupMessageWindow.Show();}}}
窗体中应用气泡提示框

        在任意窗体中使用气泡提示管理类中气泡提示框方法,设置其信息和信息类型,在该窗体中显示气泡提示。

    /// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{/// <summary>/// 消息提醒管理/// </summary>public MessageManager MessageManager {  get { return new MessageManager(); } }public MainWindow(){InitializeComponent();}private void btnTip_Click(object sender, RoutedEventArgs e){MessageManager.ShowMessageTip("当前资源为最新", this, 1);}}

气泡提示框项目实例效果

项目实例链接:https://download.csdn.net/download/lvxingzhe3/88677901

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

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

相关文章

【neo4j】neo4j的安装与使用

【neo4j】neo4j的安装与使用 安装java https://www.oracle.com/java/technologies/downloads/ 按照步骤安装即可 配置环境变量 在系统变量中添加 path变量中添加 安装neo4j https://neo4j.com/deployment-center/ 下载后&#xff0c;在指定位置解压缩 与java相同&#…

【源码】-MyBatis-如何系统地看源码

写在前面 前段时间做过一个项目&#xff0c;期间用到了动态数据源dynamic-datasource&#xff0c;经历了dbcp2的数据库连接池没有生效到排查定位、MyBatis多种数据库产品兼容、手写MyBatis拦截器等事情。 花费了好久&#xff0c;一直在打磨这篇文章&#xff08;不知道花费这么长…

面试题:说一下Spring 中的 @Cacheable 缓存注解?

文章目录 1 什么是缓存2 本地缓存和集中式缓存3 本地缓存的优点4 Spring对于缓存的支持4.1 spring支持的CacheManager4.2 GuavaCache4.3 引入依赖4.4 创建配置类4.5 缓存注解4.6 Cacheable的用法 5 Cacheable失效的原因 1 什么是缓存 第一个问题&#xff0c;首先要搞明白什么是…

如何在MAC OS中的XCODE下添加 <bits/stdc++.h>

mac上使用的编译器是Clang&#xff0c;但是没有万能头文件bits/stdc.h\&#xff0c;本文介绍如何添加万能头文件 Xcode 版本&#xff1a;15.1 - 打开应用程序-Xcode-右键显示包内容 Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/includ…

【React源码 - ReactDom.render发生了什么】

在React开发中&#xff0c;在入口文件我们都会执行ReactDom.render来讲整个应用挂载在主document中&#xff0c;那其中发生了什么&#xff0c;React是如何讲我们写的JSX代码&#xff0c;一步一步更新Fiber进而挂载渲染的呢。本文主要是基于react17.0.2的源码以及自己的理解来简…

NFC物联网智慧校园解决方案

近场通信(Near Field Communication&#xff0c;NFC)又称近距离无线通信&#xff0c;是一种短距离的高频无线通信技术&#xff0c;允许电子设备之间进行非接触式点对点数据传输交换数据。这个技术由免接触式射频识别(RFID)发展而来&#xff0c;并兼容 RFID&#xff0c;主要用于…

【开源】基于Vue+SpringBoot的房屋出售出租系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 房屋销售模块2.2 房屋出租模块2.3 预定意向模块2.4 交易订单模块 三、系统展示四、核心代码4.1 查询房屋求租单4.2 查询卖家的房屋求购单4.3 出租意向预定4.4 出租单支付4.5 查询买家房屋销售交易单 五、免责说明 一、摘…

【Redis-01】RedisObject基本原理及各属性结构的作用

我们知道&#xff0c;redis常用的5种类型底层都是通过redisObject去封装的。看一下redisObject的源码&#xff1a; typedef struct redisObject {unsigned type:4;unsigned encoding:4;unsigned lru:LRU_BITS; int refcount;void *ptr; } robj;这几个属性都很重要&#xff0c;…

ubuntu python播放MP3,wav音频和录音

目录 一.利用pygame&#xff08;略显麻烦&#xff0c;有时候播放不太正常&#xff09;1.安装依赖库2.代码 二.利用mpg123&#xff08;简洁方便&#xff0c;但仅争对mp3&#xff09;1.安装依赖库2.代码 三.利用sox&#xff08;简单方便&#xff0c;支持的文件格式多&#xff09;…

YOLO训练results.csv文件可视化(原模型与改进模型对比可视化)

一、单独一个文件可视化&#xff08;源码对应utils文件夹下的plots.py文件的plot_results类&#xff09; from pathlib import Path import matplotlib.pyplot as plt import pandas as pd def plot_results(fileruns/train/exp9/results.csv, dir):# Plot training results.c…

图像的颜色及Halcon颜色空间转换transfrom_rgb/trans_to_rgb/create_color_trans lut

图像的颜色及Halcon颜色空间转换 文章目录 图像的颜色及Halcon颜色空间转换一. 图像的色彩空间1. RGB颜色 2. 灰度图像3. HSV/ HSI二. Bayer 图像三. 颜色空间的转换1. trans_from_rgb算子2. trans_to_rgb算子3. create_color_trans_lut算子 图像的颜色能真实地反映人眼所见的真…

python+django网上购物商城系统o9m4k

语言&#xff1a;Python 框架&#xff1a;django/flask可以定制 软件版本&#xff1a;python3.7.7 数据库&#xff1a;mysql 数据库工具&#xff1a;Navicat 开发工具pycharm/vscode都可以 前端框架:vue.js 系统使用过程主要涉及到管理员和用户两种角色&#xff0c;主要包含个…