WPF 自定义一个MessageBox控件
程序员文章站
2022-03-04 11:49:50
...
WPF 自定义一个MessageBox控件
先看效果:
新建一个WPF窗体UMessageBox:
xaml代码:
<Window x:Class="WPF20201123.Views.UMessageBox"
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:WPF20201123.Views"
mc:Ignorable="d"
Title="UMessageBox" Height="150" Width="350" 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>
<Style x:Key="btn_Close" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="FontSize" Value="18"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Opacity" Value="0.7"/>
</Trigger>
<!--<Trigger Property="IsMouseOver" Value="False">
<Setter Property="Background" Value="#EEF0F5"/>
</Trigger>-->
</Style.Triggers>
</Style>
<Style x:Key="BtnInfoStyle" TargetType="Button">
<Setter Property="Width" Value="70"/>
<Setter Property="Height" Value="25"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Background" Value="#43a9c7"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" CornerRadius="2" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
<TextBlock Text="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<!--<Setter TargetName="border" Property="Background" Value="#2f96b4"/>-->
<Setter TargetName="border" Property="Opacity" Value="0.8"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<!--<Setter TargetName="border" Property="Background" Value="#2a89a4"/>-->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Border Margin="5" Background="White" CornerRadius="0">
<Border.Effect>
<DropShadowEffect Color="Gray" ShadowDepth="0" BlurRadius="10" Opacity="0.3" Direction="0"/>
</Border.Effect>
<Grid>
<!--内容区-->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--Background="#EEF0F5"-->
<Grid Grid.Row="0" Name="panel_Title" Background="#3C6AB1" MouseLeftButtonDown="panel_Title_MouseLeftButtonDown">
<TextBlock x:Name="lblTitle" Text="提示" Foreground="White" FontSize="14" Margin="10 0 0 0" VerticalAlignment="Center"/>
<Button x:Name="btn_Close" Content="✕" Style="{StaticResource btn_Close}" HorizontalAlignment="Right" Foreground="White" Margin="0 0 5 0" Height="30" Width="30" Click="btn_Close_Click"/>
</Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="1">
<TextBlock Text="" FontFamily="/Fonts/#iconfont" Foreground="#3C6AB1" FontSize="18" VerticalAlignment="Center" Margin="0 0 5 0"/>
<TextBlock x:Name="lblMsg" Text="欢迎使用!" Foreground="#666666" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
<!--<Button Width="60" Height="25" Content="取消" Margin="0 0 10 0" Style="{StaticResource BtnInfoStyle}" Background="Gray" Name="btn_Cancel" Click="btn_Cancel_Click"/>-->
<Button Width="60" Height="25" Content="确定" Margin="0 0 10 0" Style="{StaticResource BtnInfoStyle}" Background="#3C6AB1" Name="btn_OK" Click="btn_OK_Click"/>
</StackPanel>
</Grid>
</Grid>
</Border>
<!--<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>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WPF20201123.Views
{
/// <summary>
/// UMessageBox.xaml 的交互逻辑
/// </summary>
public partial class UMessageBox : Window
{
public 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();
//}
private void btn_Cancel_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
//this.Close();
}
private void btn_OK_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
//this.Close();
}
private void panel_Title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ButtonState == MouseButtonState.Pressed)
{
this.DragMove();
}
}
private void btn_Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
使用方法:
UMessageBox.Show("提示:", "欢迎使用!");
或
bool? result = UMessageBox.Show("提示:", "导出成功!是否用IDE运行?");
if(result == true)
{
//其它
}
推荐阅读
-
WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展
-
[WPF自定义控件库] 模仿UWP的ProgressRing
-
如何在双向绑定的Image控件上绘制自定义标记(wpf)
-
[WPF自定义控件库] 给WPF一个HyperlinkButton
-
[WPF自定义控件库]使用TextBlockHighlightSource强化高亮的功能,以及使用TypeConverter简化调用
-
WPF自定义实现IP地址输入控件
-
wpf 两个自定义控件
-
WPF的ListView控件自定义布局用法实例
-
[WPF 学习] 9.自定义一个Window的样子
-
WPF自定义控件之图片控件 AsyncImage