WPF自定义一个MessageBox介绍

xaml:

<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>


csharp:

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();

        }

    }

}


调用:

if (UMessageBox.Show("提示", “消息") == true)
                {
                    
                }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 提示对话框样式是指对应用程序中的弹出提示信息框进行自定义样式设计。WPF 提供了丰富的组件和属性来实现对话框的个性化设置。 首先,我们可以使用样式(Style)来修改对话框的外观。通过为对话框中的控件应用自定义样式,我们可以改变按钮的颜色、边框的样式、字体的大小和颜色等。可以使用预定义的样式,也可以自定义样式以满足特定需求。 其次,WPF 提供了弹出动画的支持,使得对话框的出现和消失能够更加平滑。开发人员可以使用内置的动画效果(如淡入淡出或滑动),或者自定义动画来实现对话框的过渡效果,增加用户体验。 此外,WPF 还提供了对话框的位置和大小进行控制的功能。我们可以调整对话框的大小,使其适应内容的长度和宽度,并确保它在屏幕上的位置合适。通过指定对话框的最小和最大大小以及位置的绑定,可以使其在不同的窗口大小上实现自适应。 最后,WPF 提供了多种选择和事件处理机制,以便与用户进行交互。开发人员可以根据不同的用户操作创建不同的对话框响应,例如单击确认按钮或取消按钮时的不同行为,以实现复杂的交互逻辑。 总结来说,WPF 提供了丰富的功能和灵活性,使开发者能够轻松地创建具有个性化样式的提示对话框。通过使用样式和动画效果以及控制大小和位置,可以实现更好的用户体验和可定制性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值