C# WPF自定义消息弹窗
我用的是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();
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?