栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

WPF 自定义一个MessageBox控件

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WPF 自定义一个MessageBox控件

WPF 自定义一个MessageBox控件

先看效果:

新建一个WPF窗体UMessageBox:
xaml代码:


    
    
        
        
    
    
    
        
            
        
        
            
            
                
                    
                    
                    
                
                
                
                    
                    

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPF20201123.Views
{
    /// 
    /// UMessageBox.xaml 的交互逻辑
    /// 
    public partial class UMessageBox : Window
    {
        public UMessageBox()
        {
            InitializeComponent();
        }

        public new string Title
        {
            get { return this.lblTitle.Text; }
            set { this.lblTitle.Text = value; }
        }

        public string Message
        {
            get { return this.lblMsg.Text; }
            set { this.lblMsg.Text = value; }
        }

        /// 
        /// 静态方法 模拟MESSAGEBOX.Show方法
        /// 
        /// 标题
        /// 消息
        /// 
        public static bool? Show(string title, string msg)
        {
            var msgBox = new UMessageBox();
            msgBox.Title = title;
            msgBox.Message = msg;
            return msgBox.ShowDialog();
        }

        //private void Yes_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    this.DialogResult = true;
        //    this.Close();
        //}


        //private void No_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    this.DialogResult = false;
        //    this.Close();
        //}

        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            //this.Close();
        }

        private void btn_OK_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            //this.Close();
        }

        private void panel_Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
            {
                this.DragMove();
            }
        }

        private void btn_Close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}

使用方法:

UMessageBox.Show("提示:", "欢迎使用!");

或

 bool? result = UMessageBox.Show("提示:", "导出成功!是否用IDE运行?");
 if(result == true)
 {
     //其它
 }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910696.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号