首页 > 编程语言 >C# WPF自定义消息弹窗

C# WPF自定义消息弹窗

时间:2024-03-25 16:22:34浏览次数:15  
标签:string 自定义 C# MsgBoxViewModel strMsg OnRequestClose strTitle public 弹窗

我用的是Caliburn Micro框架,自建框架或者使用其它框架的可自行替换绑定部分即可。

效果图:

 消息窗体View代码:

<Window x:Class="WpfAppTest.Views.MsgBoxView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfAppTest.Views"
        xmlns:cm="http://caliburnmicro.com"
        mc:Ignorable="d"
        Title="MsgBoxView" Height="200" Width="400" WindowStartupLocation="CenterScreen" WindowStyle="None" ResizeMode="CanMinimize">
    <Window.Background>
        <RadialGradientBrush GradientOrigin="0.5,-0.8" Center="0.5,0" RadiusX="0.7" RadiusY="0.7">
            <GradientStop Color="#a20b40" Offset="0"/>
            <GradientStop Color="#08113c" Offset="1"/>
        </RadialGradientBrush>
    </Window.Background>
    <Window.Resources>
        <Style x:Key="LongButtonStyle" TargetType="Button">
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="Background" Value="#08113c"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="BorderBrush" Value="White"/>
            <Setter Property="BorderThickness" Value="2"/>
            <Setter Property="Height" Value="25"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border x:Name="border"   
                                BorderBrush="{TemplateBinding BorderBrush}"   
                                BorderThickness="{TemplateBinding BorderThickness}"   
                                Background="{TemplateBinding Background}"   
                                SnapsToDevicePixels="True"  
                                CornerRadius="10">
                            <ContentPresenter x:Name="contentPresenter"  
                                            Content="{TemplateBinding Content}"  
                                            ContentTemplate="{TemplateBinding ContentTemplate}"  
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"  
                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  
                                            Margin="{TemplateBinding Padding}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="Background" Value="LightCoral"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="border" Property="Background" Value="DarkRed"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="Gray"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="15*"/>
            <RowDefinition Height="70*"/>
            <RowDefinition Height="20*"/>
        </Grid.RowDefinitions>
        <Label Content="{Binding StrTitle}" Grid.Row="0" HorizontalContentAlignment="Center" VerticalContentAlignment="Top" FontSize="15" FontWeight="Bold" Foreground="Orange"/>
        <TextBlock Text="{Binding StrMsg}" Grid.Row="1" Width="380" TextWrapping="Wrap" TextAlignment="Center" VerticalAlignment="Center" FontSize="18" FontWeight="Bold" Foreground="Orange"/>
        <Button Grid.Row="2" Content="确定" Width="100" cm:Message.Attach="SureClick" Style="{StaticResource LongButtonStyle}"/>
    </Grid>
</Window>

消息窗体cs代码:

    public partial class MsgBoxView : Window
    {
        public MsgBoxView(MsgBoxViewModel viewModel)
        {
            InitializeComponent();
            this.DataContext = viewModel;
            viewModel.OnRequestClose += ViewModel_OnRequestClose;
        }

        private void ViewModel_OnRequestClose(object sender, EventArgs e)
        {
            this.Close();
        }
    }

消息窗体ViewModel代码:

    public class MsgBoxViewModel : PropertyChangedBase
    {
        private string _strTitle;
        public string StrTitle
        {
            get { return _strTitle; }
            set { Set(ref _strTitle, value, nameof(StrTitle)); }
        }

        private string _strMsg;
        public string StrMsg
        {
            get { return _strMsg; }
            set { Set(ref _strMsg, value, nameof(StrMsg)); }
        }

        public MsgBoxViewModel(string strTitle, string strMsg)
        {
            StrTitle = strTitle;
            StrMsg = strMsg;
        }
        public event EventHandler OnRequestClose;
        public void SureClick()
        {
            OnRequestClose?.Invoke(this, EventArgs.Empty);
        }
    }

调用弹窗:

            MsgBoxViewModel msgBoxViewModel = new MsgBoxViewModel("警告","纯属测试");
            MsgBoxView msgBoxView = new MsgBoxView(msgBoxViewModel);
            msgBoxView.ShowDialog();

 

标签:string,自定义,C#,MsgBoxViewModel,strMsg,OnRequestClose,strTitle,public,弹窗
From: https://www.cnblogs.com/blossomwei/p/18094684

相关文章

  • [CISCN 2019东北]PWN2
    下载好附件之后,先丢到checksec看一下开了什么保护有栈溢出:Stack:Nocanaryfound丢到IDE看一下按shift+f12看一下字符串,发现没有system和/bin/sh回到上方标签(IDAView-A)回到主界面按f5查看伪代码发现encrypt()函数存在gets溢出gets没有任何限制,但是储存用户......
  • dirseach目录扫描工具-安装详细教程
    安装:1、github源码下载解压使用git安装:推荐gitclonehttps://github.com/maurosoria/dirsearch.git--depth1zip文件安装;https://codeload.github.com/maurosoria/dirsearch/zip/refs/heads/masterdocker安装:详细步骤见文:https://github.com/maurosoria/dirsearch?ta......
  • 高抗干扰/抗噪液晶驱动芯片VK2C23A/B 永嘉原厂LCD液晶段码屏驱动芯片适用于导轨电表,早
    VK2C23A/B概述:      VK2C23A/B是一个点阵式存储映射的LCD驱动器,可支持最大224点(56SEGx4COM)或者最大416点(52SEGx8COM)的LCD屏。单片机可通过I2C接口配置显示参数和读写显示数据,也可通过指令进入省电模式。其高抗干扰,低功耗的特性适用于水电气表以及工控仪表类产品。特点:•......
  • CTS测试流程
     CTS测试条件和步骤:需要使用linux系统主机需要主机连接外网跑cts需要在本地有AndroidSDK环境,最好要对应Android大版本需要到https://source.android.google.cn/docs/compatibility/cts/downloads?hl=zh-cn下载对应测试套件。linux电脑应下载arm类别的测试套件(约4Gb)执行命令......
  • Alibaba Cloud Linux 安装 PostgreSQL 数据库
    https://blog.csdn.net/simpledate/article/details/123365889?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2-123365889-blog-135289834.235%5Ev43%5Epc_blog_bottom_relevance_base4&depth_1-utm_so......
  • React Native 应用打包上架
    引言在将ReactNative应用上架至AppStore时,除了通常的上架流程外,还需考虑一些额外的优化策略。本文将介绍如何通过配置AppTransportSecurity、ReleaseScheme和启动屏优化技巧来提升ReactNative应用的上架质量和用户体验。配置AppTransportSecurity在iOS9引入的......
  • 高性能JavaScript——6、快速响应的用户界面
    大多数浏览器让一个单线程共用于执行JavaScript和更新用户界面。每个时刻只能执行其中一种操作,这意味着当JavaScript代码正在执行时用户界面无法响应输入,反之亦然。当JavaScript代码执行时,用户界面处于“锁定”状态。管理好JavaScript的运行时间对Web应用的性能非常重要。......
  • ChatGPTGPT4科研应用、数据分析与机器学习、论文高效写作、AI绘图技术教程
    原文链接:ChatGPTGPT4科研应用、数据分析与机器学习、论文高效写作、AI绘图技术教程https://mp.weixin.qq.com/s?__biz=MzUzNTczMDMxMg==&mid=2247598798&idx=2&sn=014f5ae90306a3b1e8fd87ab58561411&chksm=fa820329cdf58a3f72799a43016b223057fd1bd022849e607d1e62193dcdc3b26b......
  • 学习人工智能:Attention Is All You Need-3-训练;结果;结论;Transformer模型相当于 E=MC^2
    5训练Training本节描述了我们模型的训练方案。5.1训练数据和批次处理TrainingDataandBatching我们在标准的WMT2014英德数据集上进行了训练,该数据集包含约450万个句子对。句子使用字节对编码[3]进行编码,其共享源-目标词汇表包含约37000个标记。对于英法翻译,我们使用......
  • 生态系统碳循环模型CENTURY 建模方法
    原文链接:生态系统碳循环模型CENTURY建模方法https://mp.weixin.qq.com/s?__biz=MzUzNTczMDMxMg==&mid=2247598976&idx=6&sn=b684175e24c5600a69033a838535078d&chksm=fa820267cdf58b71cb1d119dfe101f7b3a312e8c0b1b8ef4adbef2894aa902b290ef7e091de2&token=1744060042&am......