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

 

posted @ 2024-03-25 16:17  芈璐  阅读(287)  评论(0编辑  收藏  举报