WPF 自定义对话框
<Window x:Class="WPFDemo2.窗体.CustomDialogWindow" 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:local="clr-namespace:WPFDemo2.窗体" xmlns:local2="clr-namespace:WPFDemo2" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Title="CustomDialogWindow" Width="800" Height="450" AllowsTransparency="True" Background="Transparent" BorderThickness="1,0,1,1" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" WindowStyle="None" mc:Ignorable="d"> <Window.Resources> <Style x:Key="msgbtnStyle" TargetType="Button"> <Setter Property="Width" Value="60" /> <Setter Property="Height" Value="25" /> <Setter Property="Margin" Value="10,0,15,0" /> </Style> </Window.Resources> <Grid Background="Transparent" MouseLeftButtonDown="Grid_MouseLeftButtonDown"> <Border Margin="5" BorderBrush="#FFA9AEB1" BorderThickness="2" CornerRadius="10"> <Border.Effect> <DropShadowEffect BlurRadius="10" Direction="300" ShadowDepth="2" Color="#FFB9BDCB" /> </Border.Effect> <Border.Background> <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"> <GradientStop Offset="0.16" Color="#FFFBFBFB" /> <GradientStop Offset="0.986" Color="#FFB4D8E2" /> </LinearGradientBrush> </Border.Background> <Grid x:Name="grid" Margin="5"> <Grid.RowDefinitions> <RowDefinition Height="30" /> <RowDefinition /> <RowDefinition Height="50" /> </Grid.RowDefinitions> <Grid Grid.Row="0" Background="Transparent"> <Border Background="Transparent" BorderBrush="#FF5FA0F1" BorderThickness="0" CornerRadius="5,5,0,0"> <Grid> <TextBlock Height="25" HorizontalAlignment="Left" Text="{Binding MessageTitle}" /> <Image Margin="0,3,3,2" HorizontalAlignment="Right" MouseLeftButtonDown="Image_MouseLeftButtonDown" Source="../Images/Close.png" /> </Grid> </Border> </Grid> <StackPanel Grid.Row="1" VerticalAlignment="Center" Orientation="Horizontal"> <Image Width="30" Height="30" Margin="40,20,20,20" Source="{Binding ImagePath}" /> <TextBlock Width="280" HorizontalAlignment="Left" VerticalAlignment="center" FontSize="12" Text="{Binding MessageBoxText}" TextAlignment="Left" TextWrapping="WrapWithOverflow" /> </StackPanel> <StackPanel Grid.Row="2" HorizontalAlignment="Right" Orientation="Horizontal"> <Button x:Name="OkButton" Click="OkButton_Click" Content="确定" IsDefault="True" Style="{StaticResource msgbtnStyle}" Visibility="{Binding OkButtonVisibility, Mode=OneWay}" /> <Button x:Name="YesButton" Click="YesButton_Click" Content="是" Style="{StaticResource msgbtnStyle}" Visibility="{Binding YesButtonVisibility, Mode=oneWay}" /> <Button x:Name="NoButton" Click="NoButton_Click" Content="否" Style="{StaticResource msgbtnStyle}" Visibility="{Binding NoButtonVisibility, Mode=OneWay}" /> <Button x:Name="CancelButton" Click="CancelButton_Click" Content="取消" Style="{StaticResource msgbtnStyle}" Visibility="{Binding CancelButtonVisibility}" /> </StackPanel> </Grid> </Border> </Grid> </Window>
public partial class CustomDialogWindow : Window { public CustomDialogWindow() { InitializeComponent(); this.DataContext = this; //所有按钮不显示() OkButtonVisibility= Visibility.Collapsed; CancelButtonVisibility= Visibility.Collapsed; YesButtonVisibility = Visibility.Collapsed; NoButtonVisibility= Visibility.Collapsed; Result=CustomMessageBoxResult.None; } public enum CustomMessageBoxButton { OK = 1, OKCancel = 2, YesNo = 3, YesNoCancel = 4 } public enum CustomMessageBoxResult { None = 0,//用户直接关闭消息框 OK = 1,//用户点击了确定按钮 Cancel = 2,//用户点击了取消按钮 Yes = 3,//用户点击了是按钮 No = 4//用户点击了否按钮 } public enum CustomMessageBoxIcon { None = 0, Error = 1, Question = 2, Infomation = 3 } public string MessageTitle { get; set; } public string MessageBoxText { get; set; } public string ImagePath { get; set; } public Visibility OkButtonVisibility { get; set; } public Visibility CancelButtonVisibility { get; set; } public Visibility YesButtonVisibility { get; set; } public Visibility NoButtonVisibility { get; set; } public CustomMessageBoxResult Result { get; set; } private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.DragMove(); } private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Close(); } public static CustomMessageBoxResult Show(string msgText, string title, CustomMessageBoxButton msgBtn, CustomMessageBoxIcon msgIcon) { CustomDialogWindow msg = new CustomDialogWindow(); msg.Topmost = true; msg.MessageBoxText = msgText; msg.MessageTitle = title; switch (msgBtn) { case CustomMessageBoxButton.OK: msg.OkButtonVisibility = Visibility.Visible; break; case CustomMessageBoxButton.OKCancel: msg.OkButtonVisibility = Visibility.Visible; msg.CancelButtonVisibility = Visibility.Visible; break; case CustomMessageBoxButton.YesNo: msg.YesButtonVisibility = Visibility.Visible; msg.NoButtonVisibility = Visibility.Visible; break; case CustomMessageBoxButton.YesNoCancel: msg.YesButtonVisibility= Visibility.Visible; msg.NoButtonVisibility = Visibility.Visible; msg.CancelButtonVisibility = Visibility.Visible; break; default: msg.OkButtonVisibility = Visibility.Visible; break; } switch (msgIcon) { case CustomMessageBoxIcon.Infomation: msg.ImagePath = AppDomain.CurrentDomain.BaseDirectory + @"/Images/Close.png"; break; case CustomMessageBoxIcon.Error: msg.ImagePath = AppDomain.CurrentDomain.BaseDirectory + @"/Images/Close.png"; break; case CustomMessageBoxIcon.Question: msg.ImagePath = AppDomain.CurrentDomain.BaseDirectory + @"/Images/Close.png"; break; } msg.ShowDialog(); return msg.Result; } private void OkButton_Click(object sender, RoutedEventArgs e) { Result = CustomMessageBoxResult.OK; this.Close(); } private void YesButton_Click(object sender, RoutedEventArgs e) { Result = CustomMessageBoxResult.Yes; this.Close(); } private void NoButton_Click(object sender, RoutedEventArgs e) { Result = CustomMessageBoxResult.No; this.Close(); } private void CancelButton_Click(object sender, RoutedEventArgs e) { Result = CustomMessageBoxResult.Cancel; this.Close(); } }
使用: //CustomDialogWindow.Show("主页面已加载完毕!","加载提示", CustomDialogWindow.CustomMessageBoxButton.OK, CustomDialogWindow.CustomMessageBoxIcon.Infomation); //CustomDialogWindow.Show("主页面已加载失败!","错误提示", CustomDialogWindow.CustomMessageBoxButton.OK, CustomDialogWindow.CustomMessageBoxIcon.Error); CustomDialogWindow.CustomMessageBoxResult result = CustomDialogWindow.Show("你确定已加载完毕了吗?","询问提示", CustomDialogWindow.CustomMessageBoxButton.OKCancel, CustomDialogWindow.CustomMessageBoxIcon.Question); if(result == CustomDialogWindow.CustomMessageBoxResult.OK) { MessageBox.Show("OK"); } else { MessageBox.Show("Cancel"); }