WPF自定义MessageBox

WPF利用window实现自定义MessageBox,并实现自定义点击事件

效果图

效果图1
效果图12

项目结构

项目结构

//类似工厂模式显示不同风格的MessageBox
 class MyMessageBox
    {
        public static int OK=0;
        public static int OKCANCLE = 1;
        public MyMessageBox(){}

        public static void Show(string mess)
        {
            MessageBoxOK myMessageBoxOK = new MessageBoxOK(mess);
            myMessageBoxOK.Show();

        }
        /// <summary>
        /// 显示对话框
        /// </summary>
        /// <param name="mess">提示消息</param>
        /// <param name="style">对话框样式</param>
        public static bool?  ShowDialog(string mess, int style)
        {
            switch (style)
            {
                case 0:
                    MessageBoxOK myMessageBoxOK = new MessageBoxOK(mess);
                    return myMessageBoxOK.ShowDialog();
                    break;
                case 1:
                    MessageBoxOKCancle myMessageBoxOKCancle = new MessageBoxOKCancle(mess);
                    return myMessageBoxOKCancle.ShowDialog();
                    break;
                default:
                    return false;
            }

        }

    }
//两个按钮的MessageBox后台代码
 public partial class MessageBoxOKCancle : Window
    {

        public MessageBoxOKCancle()
        {
            InitializeComponent();
        }

        public MessageBoxOKCancle(string mess)
        {
            InitializeComponent();
            message.Text = mess;
        }

        private void Confirm_Click(object sender, RoutedEventArgs e)
        {
        //如果window不是以Showdialog方式打开设置DialogResult时会引发异常
            try
            {
                this.DialogResult = true;
            }
            catch (Exception ex) { }
            this.Close();
        }
        private void Cancle_Click(object sender, RoutedEventArgs e)
        {
        //如果window不是以Showdialog方式打开设置DialogResult时会引发异常
            try
            {
                this.DialogResult = false;
            }
            catch (Exception ex) { }
            this.Close();
        }
    }
//MessageBox界面
<Window x:Class="AutoShopManager.Managers.Messagebox.MessageBoxOKCancle"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="提示" Height="300" Width="350"  WindowStyle="None" WindowStartupLocation="CenterScreen">
    <Grid>
        <TextBlock Name="message" HorizontalAlignment="Left" TextAlignment="Center" TextBlock.FontSize="25" Margin="31,69,0,0" TextWrapping="Wrap" Text="支付成功" VerticalAlignment="Top" Height="123" Width="277"/>
        <Button Content="确定" HorizontalAlignment="Left" Margin="190,212,0,0" VerticalAlignment="Top" Width="75" Click="Confirm_Click"/>
        <Button Content="取消" HorizontalAlignment="Left" Margin="83,212,0,0" VerticalAlignment="Top" Width="75" Click="Cancle_Click"/>
    </Grid>
</Window>
//单个按钮的MessageBox后台代码
        public MessageBoxOK()
        {
            InitializeComponent();
        }
        public MessageBoxOK(string mess)
        {
            InitializeComponent();
            message.Text = mess;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try { 
                this.DialogResult = false; 
            }
            catch(Exception ex){ }

            this.Close();
        }
    }
<Window x:Class="AutoShopManager.Managers.Messagebox.MessageBoxOK"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="提示" Height="300" Width="350"  WindowStyle="None" WindowStartupLocation="CenterScreen">
    <Grid>
        <TextBlock Name="message" HorizontalAlignment="Left" TextAlignment="Center" TextBlock.FontSize="25" Margin="31,69,0,0" TextWrapping="Wrap" Text="支付成功" VerticalAlignment="Top" Height="123" Width="277"/>
        <Button Content="确定" HorizontalAlignment="Left" Margin="134,220,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>

使用方式

if(MyMessageBox.ShowDialog("种类名字不得为空", MyMessageBox.OKCANCLE).Value == true)
                {
                    MessageBox.Show("ok");
                }
                else
                {
                    MessageBox.Show("No");
                }

注意:

window如果以showdialog的形式启动所有线程都会停止,直到这个窗口返回,才会执行后面的代码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值