前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF自定义一个MessageBox

WPF自定义一个MessageBox

作者头像
MJ.Zhou
发布2022-05-07 16:20:00
7700
发布2022-05-07 16:20:00
举报
文章被收录于专栏:.NET开发那点事.NET开发那点事

WPF的MessageBox太丑了,自己定义了一个。

这是效果:

XAML:

代码语言:javascript
复制
<Window x:Class="AgileToDo.UMessageBox"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="UMessageBox" Height="130" Width="280" WindowStyle="None"   Background="{x:Null}"  
        ResizeMode="NoResize"  x:Name="main"
        AllowsTransparency="True" WindowStartupLocation="CenterScreen">
    <Window.Triggers>
        <EventTrigger RoutedEvent="Window.Loaded" >
            <BeginStoryboard>
                <Storyboard Name="sbOpShow">
                    <DoubleAnimation
                    Storyboard.TargetName="main" 
                    Storyboard.TargetProperty="Opacity" 
                    From="0" To="0.8" Duration="0:0:0.15"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Window.Triggers>
    <Window.Resources>
    </Window.Resources>
    <Border Background="#F03A3A3A" Opacity="1" CornerRadius="3">
            <Canvas>
                
            <Border Canvas.Top="0" Height="25" Width="{Binding ElementName=main, Path=Width,UpdateSourceTrigger=PropertyChanged}" 
                    BorderThickness="0,0,0,1" BorderBrush="#FFEFE2E2">
                <TextBlock x:Name="lblTitle" Text="test" HorizontalAlignment="Left" 
                           Foreground="#FFCBBEBE" FontSize="14"
                           VerticalAlignment="Center" Margin="5"/>
            </Border>
            <TextBlock x:Name="lblMsg"
                       Foreground="White" FontSize="14" 
                       TextWrapping="Wrap" Text="test"
                       HorizontalAlignment="Center" Canvas.Left="20"
                       Canvas.Top="50" 
                       />
            <Border BorderBrush="#FF60C1C1" BorderThickness="0.5" Height="22" 
                    Canvas.Bottom="10" Canvas.Right="85" MouseLeftButtonDown="Yes_MouseLeftButtonDown"
                 Name="border1" Width="49" Background="#FFC7C7C7" CornerRadius="2">
                <TextBlock Text="YES"  HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
            <Border BorderBrush="#FF60C1C1" BorderThickness="0.5" Height="22" 
                    Canvas.Bottom="10" Canvas.Right="30" MouseLeftButtonDown="No_MouseLeftButtonDown"
                Name="border2" Width="49" CornerRadius="2" Background="#FFC7C7D1">
                <TextBlock Text="NO"  HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </Canvas>
    </Border>
</Window>

 CS:

代码语言:javascript
复制
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Animation;

namespace AgileToDo
{
    /// <summary>
    /// UMessageBox.xaml 的交互逻辑
    /// </summary>
    public partial class UMessageBox : Window
    {
        /// <summary>
        /// 禁止在外部实例化
        /// </summary>
        private 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; }
        }
        /// <summary>
        /// 静态方法 模拟MESSAGEBOX.Show方法
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="msg">消息</param>
        /// <returns></returns>
        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();
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档