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.mfbz.cn/a/279463.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Vue学习day_03

普通组件的注册 局部注册: 创建一个components的文件夹 在里面写上对应的.vue文件 在对应的vue里面写上对应的3部分 template写上对应的核心代码 盒子等 style 写上对应的css修饰 在App.vue里面进行引用 import 导包 格式是 import 起个名字 from 位置 在写一个component…

javaweb基础2.0 (持续更新中)

Day 10 : Responbody 将响应的实体类转为json发送给前端。RequestBody将request的json转为实体类给后端 修改未成功的可能是因为根据id 查询的时候&#xff0c;只查询了name字段&#xff0c;因为后面需要根据id 修改&#xff0c;所以还需查询id &#xff0c;不然前端不知道id也…

回归预测 | Python实现OOA-LightGBM基于人工鱼鹰优化算法优化LightGBM的多输入单输出数据回归预测模型 (多指标,多图)

回归预测 | Python实现OOA-LightGBM基于人工鱼鹰优化算法优化LightGBM的多输入单输出数据回归预测模型 &#xff08;多指标&#xff0c;多图&#xff09; 目录 回归预测 | Python实现OOA-LightGBM基于人工鱼鹰优化算法优化LightGBM的多输入单输出数据回归预测模型 &#xff08;…

STM32 ESP8266 物联网智能温室大棚 源码PCB原理图 设计文档

资料下载: https://download.csdn.net/download/vvoennvv/88680924 一、概述 本系统以STM32F103C8T6单片机为主控芯片&#xff0c;采用相关传感器构建系统硬件电路。其中使用DHT11温湿度传感器对温度和湿度的采集&#xff0c;MQ-7一氧化碳传感器检测CO浓度&#xff0c;GP2Y101…

nginx日志常见报错解决

目录 一&#xff1a;报错 二&#xff1a;php查看后台内容有的栏目出现502&#xff1f; 三&#xff1a;413 Request Entity Too Large? 四&#xff1a;Request Header Or Cookie Too Large 400 一&#xff1a;报错 upstream prematurely closed connection while reading r…

Java之程序、进程、线程、管程和并发、并行的概念

文章目录 1. 进程与线程1.1 程序1.2 进程1.3 线程1.4 管程 2.并行与并发2.1 并发2.2 并行 1. 进程与线程 1.1 程序 程序是指令和数据的有序集合&#xff0c;其本身没有任何运行的含义&#xff0c;是一个静态的概念。简单的说就是我们写的代码。 1.2 进程 &#xff08;1&…

C# 使用ZXing.Net识别二维码和条码

目录 写在前面 代码实现 调用示例 写在前面 上一篇写了 C# 使用ZXing.Net生成二维码和条码-CSDN博客 使用ZXing.Net解码非常简单&#xff0c;事实上就只用一行代码就好了&#xff0c;这么简单那为什么还要贴在这里呢&#xff0c;原因是开始时&#xff0c;在网上看资料看到…

【项目管理】CMMI-项目总结报告模版

1、文档目录结构 2、计划与实际情况对比 3、开放工作评价

【每日一题】一周中的第几天

文章目录 Tag题目来源解题思路方法一&#xff1a;模拟 写在最后 Tag 【模拟】【数学】【2023-12-30】 题目来源 1185. 一周中的第几天 解题思路 方法一&#xff1a;模拟 思路 题目中的日期是在 1971 到 2100 年之间的有效日期&#xff0c;即 1971-01-01 到 2100-12-31 范围…

ssm基于web的志愿者管理系统的设计与实现+vue论文

摘 要 使用旧方法对志愿者管理系统的信息进行系统化管理已经不再让人们信赖了&#xff0c;把现在的网络信息技术运用在志愿者管理系统的管理上面可以解决许多信息管理上面的难题&#xff0c;比如处理数据时间很长&#xff0c;数据存在错误不能及时纠正等问题。这次开发的志愿者…

GitHub 一周热点汇总 第3期 (2023/12/24-12/30)

GitHub一周热点汇总第三期 (2023/12/24-12/30)&#xff0c;梳理每周热门的GitHub项目&#xff0c;了解热点技术趋势&#xff0c;掌握前沿科技方向&#xff0c;发掘更多商机。元旦就要到了&#xff0c;提前祝大家新年快乐。 #1 StreamDiffusion 项目名称&#xff1a;StreamDiff…

AIGC开发:调用openai的API接口实现简单机器人

简介 开始进行最简单的使用&#xff1a;通过API调用openai的模型能力 OpenAI的能力如下图&#xff1a; 文本生成模型 OpenAI 的文本生成模型&#xff08;通常称为生成式预训练 Transformer 或大型语言模型&#xff09;经过训练可以理解自然语言、代码和图像。这些模型提供文…

Maven项目提示Ignored pom.xml问题

1 环境 &#xff08;1&#xff09;IDEA开发工具&#xff1a;2022.2.1 &#xff08;2&#xff09;JDK&#xff1a;Java17&#xff08;Spring6要求JDK最低版本是Java17&#xff09; &#xff08;3&#xff09;Spring&#xff1a;6.1.2 &#xff08;4&#xff09;Maven 3.8.8 2 …

vscode 支持c,c++编译调试方法

概述&#xff1a;tasks.jason launch.json settings.json一定要有&#xff0c;没有就别想跑。还有就是c 和c配置有区别&#xff0c;切记&#xff0c;下文有说 1.安装扩展插件。 2.安装编译器&#xff0c;gcc.我用的是x86_64-8.1.0-release-win32-seh-rt_v6-rev0.7z &#xf…

力扣LeetCode第80题 删除有序数组中的重复项 II

一、题目 给你一个有序数组 nums &#xff0c;请你 原地 删除重复出现的元素&#xff0c;使得出现次数超过两次的元素只出现两次&#xff0c;返回删除后数组的新长度。 不要使用额外的数组空间&#xff0c;你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 示…

后端程序员React初接触1

后端程序员React初接触 学习react基础与相关库的使用学习 包括react基础 路由 组件库等等 react是用于构建用户界面的JavaScript库 发送请求获取数据处理数据操作dom呈现页面&#xff08;react帮忙操作dom&#xff09; 数据渲染为视图 有facebook打造并开源 解决的问题 dom操…

【教学类-43-05】 不可用 20231229 N宫格数独5.0(n=1-9) (ChatGPT AI对话大师生成 随机数字填空 )

说明&#xff1a;本代码使用“”随机数字填空”&#xff0c;结果有误差 不能使用 背景需求&#xff1a; 大4班20号说&#xff1a;我不会做这种&#xff08;九宫格&#xff09;&#xff0c;我做的是小格子的&#xff0c; 他把手工纸翻过来&#xff0c;在反面自己画了矩阵格子。…

FPGA设计时序约束十四、Set_External_Delay

一、序言 在时序约束中对clock的约束还存在一种特殊的延时约束set external delay。set external delay如字面含义&#xff0c;设置外部的时延值&#xff0c;但这个外部时延主要是指反馈时延&#xff0c;即信号从FPGA的output端口输出后经过外部电路回到输入端口的时延值。 二…

频谱论文:基于空间稀疏采样的频谱态势生成: 模型与算法

#频谱# 张国勇,王军,陈霄南等.基于空间稀疏采样的频谱态势生成:模型与算法[J].中国科学:信息科学,2022,52(11):2011-2036. &#xff08;电子科技大学&#xff09; 摘要 面对日益复杂的电磁频谱环境和持续增长的用频需求, 为了维护电磁频谱秩序和安全, 提高频谱资源整体利用效率…

Java的继承和实现、接口和抽象类,它们的区别?

之前我写过Java的继承和多态的用处&#xff0c;这次讲讲继承和实现&#xff0c;接口和抽象类的区别&#xff0c;这其实是个很基础的Java知识&#xff0c;但是真的非常重要。特别是在阅读源码的时候&#xff0c;源码里面用了大量的继承和实现&#xff0c;接口以及抽象类&#xf…
最新文章