自定义的消息弹出框

有时候系统自带的消息弹出框不能满足我们的要求,这就需要自己定义弹出框,下面是自己定义的弹出框,以备查用

首先要定义一个窗体弹出框:

xaml如下:

<Window x:Class="CoffeeMachine.Controls.Message"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" WindowState="Maximized" WindowStyle="None" AllowsTransparency="True"
             d:DesignHeight="1920" d:DesignWidth="1080" Background="Transparent">

    <Viewbox>
        <Grid Height="1920" Width="1080">
            <Grid>
                <Border Background="Black" Opacity="0.5"></Border>
                <Border CornerRadius="20" Width="600" MinHeight="200" HorizontalAlignment="Center" VerticalAlignment="Center" Background="#D9FFFFFF" >
                    <Grid Width="600" MinHeight="200">
                        <TextBlock Name="tbTitle" FontSize="35" Foreground="#FFFF8700" Text="提示信息" HorizontalAlignment="Center" VerticalAlignment="Top" Height="50" Margin="0,20"/>

                        <StackPanel Margin="0,100,0,0">
                            <TextBlock x:Name="tbContent" HorizontalAlignment="Center" TextWrapping="Wrap" MaxWidth="500" FontSize="35"/>
                            <Button x:Name="btnOk" Margin="100,30" Content="关闭" FontSize="35" Foreground="White" Style="{StaticResource ButtonStyle}" Height="80" Click="Button_Click"/>
                            <StackPanel Name="spOkCancel" Margin="50,30" Orientation="Horizontal" Visibility="Collapsed">
                                <Button Content="确定" FontSize="35" Foreground="White" Style="{StaticResource ButtonStyle}" Height="80" Width="225" Click="Button_Click_1"/>
                                <Button Content="取消" FontSize="35" Foreground="White" Style="{StaticResource ButtonStyle}" Height="80"  Width="225" Margin="50,0,0,0" Click="Button_Click_2"/>
                            </StackPanel>
                        </StackPanel>
                    </Grid>
                </Border>
            </Grid>
        </Grid>
    </Viewbox>
</Window>

.cs如下:

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.Navigation;
using System.Windows.Shapes;

namespace CoffeeMachine.Controls
{
    /// <summary>
    /// Message.xaml 的交互逻辑
    /// </summary>
    public partial class Message : Window
    {

        public Message(string message,string title,MessageBoxButton messageBoxButton)
        {
            InitializeComponent();
            MessageType(messageBoxButton);
            tbTitle.Text = title;
            tbContent.Text = message;
        }

        private void MessageType(MessageBoxButton messageBoxButton)
        {
            if (messageBoxButton == MessageBoxButton.OKCancel)
            {
                btnOk.Visibility = Visibility.Visible;
                spOkCancel.Visibility = Visibility.Visible;
            }
        }


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

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

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

    }
}

 

然后使用消息框的时候,定义一个静态类来引用该消息框。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CoffeeMachine.ClassHelp
{
    public static class MessageHelper
    {
        public static void ShowMessage(string message,string title="",MessageBoxButton ms=MessageBoxButton.OK)
        {
            Controls.Message messageBox = new Controls.Message(message,title,ms);
            messageBox.ShowDialog();
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值