Przeglądaj źródła

添加自定义MessageBox

宝臣 王 3 miesięcy temu
rodzic
commit
b7ab5463d7

+ 1 - 0
UniformMaterialManagementSystem.FilterDataGrid/FilterDataGrid.cs

@@ -508,6 +508,7 @@ namespace FilterDataGrid
                         Padding = new Thickness(0, 0, 4, 0),
                         Margin = new Thickness(2.0)
                     };
+
                     txt.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                     RowHeaderWidth = Math.Max(Math.Ceiling(txt.DesiredSize.Width),
                         RowHeaderWidth >= 0 ? RowHeaderWidth : 0);

+ 206 - 0
UniformMaterialManagementSystem/Custom/MessageBoxEx.xaml

@@ -0,0 +1,206 @@
+<Window
+    x:Class="UniformMaterialManagementSystem.Custom.MessageBoxEx"
+    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"
+    Title="MessageBoxEx"
+    Width="440"
+    MinHeight="180"
+    MaxHeight="300"
+    AllowsTransparency="True"
+    Background="Transparent"
+    FontSize="14"
+    Foreground="#201f1e"
+    Loaded="Window_Loaded"
+    ResizeMode="NoResize"
+    SizeToContent="Height"
+    WindowStartupLocation="CenterOwner"
+    WindowStyle="None"
+    mc:Ignorable="d">
+    <Window.Resources>
+        <Style x:Key="MessageBoxButtonStyle" TargetType="Button">
+            <Setter Property="Background" Value="White" />
+            <Setter Property="BorderBrush" Value="#8a8886" />
+            <Setter Property="Foreground" Value="#201f1e" />
+        </Style>
+    </Window.Resources>
+    <Border
+        Margin="12"
+        Background="{Binding BackgroundColor}"
+        BorderBrush="#a19f9d"
+        BorderThickness="1"
+        CornerRadius="8">
+        <Border.Style>
+            <Style TargetType="Border">
+                <Style.Triggers>
+                    <DataTrigger Binding="{Binding IsEnabledEffect}" Value="true">
+                        <Setter Property="Effect">
+                            <Setter.Value>
+                                <DropShadowEffect
+                                    BlurRadius="6"
+                                    Opacity="0.4"
+                                    ShadowDepth="0" />
+                            </Setter.Value>
+                        </Setter>
+                    </DataTrigger>
+                </Style.Triggers>
+            </Style>
+        </Border.Style>
+        <DockPanel>
+            <!--#region Buttons-->
+            <Grid DockPanel.Dock="Bottom">
+                <StackPanel
+                    Name="ButtonsArea"
+                    Margin="0,15,20,15"
+                    HorizontalAlignment="Right"
+                    Orientation="Horizontal">
+                    <Button
+                        Name="LeftButton"
+                        Height="30"
+                        MinWidth="80"
+                        d:Content="Left"
+                        Click="Button_Click"
+                        Style="{StaticResource MessageBoxButtonStyle}">
+                        <Button.Resources>
+                            <Style TargetType="Border">
+                                <Setter Property="CornerRadius" Value="4" />
+                            </Style>
+                        </Button.Resources>
+                    </Button>
+                    <Button
+                        Name="MiddleButton"
+                        Height="30"
+                        MinWidth="80"
+                        Margin="4,0,0,0"
+                        d:Content="Middle"
+                        Click="Button_Click"
+                        Style="{StaticResource MessageBoxButtonStyle}">
+                        <Button.Resources>
+                            <Style TargetType="Border">
+                                <Setter Property="CornerRadius" Value="4" />
+                            </Style>
+                        </Button.Resources>
+                    </Button>
+                    <Button
+                        Name="RightButton"
+                        Height="30"
+                        MinWidth="80"
+                        Margin="4,0,0,0"
+                        d:Content="Right"
+                        Click="Button_Click"
+                        Style="{StaticResource MessageBoxButtonStyle}">
+                        <Button.Resources>
+                            <Style TargetType="Border">
+                                <Setter Property="CornerRadius" Value="4" />
+                            </Style>
+                        </Button.Resources>
+                    </Button>
+                </StackPanel>
+            </Grid>
+            <!--#endregion-->
+            <!--#region Icon, Message-->
+            <Grid>
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition Width="Auto" />
+                    <ColumnDefinition />
+                </Grid.ColumnDefinitions>
+
+                <StackPanel Margin="0,0,0,0" Orientation="Horizontal">
+                    <Viewbox
+                        Width="48"
+                        Height="48"
+                        Margin="18,10,20,0"
+                        VerticalAlignment="Top">
+                        <Grid>
+                            <Path
+                                Name="ErrorIcon"
+                                d:Visibility="Visible"
+                                Data="M10 2a8 8 0 1 1 0 16a8 8 0 0 1 0-16zM7.81 7.114a.5.5 0 0 0-.638.058l-.058.069a.5.5 0 0 0 .058.638L9.292 10l-2.12 2.121l-.058.07a.5.5 0 0 0 .058.637l.069.058a.5.5 0 0 0 .638-.058L10 10.708l2.121 2.12l.07.058a.5.5 0 0 0 .637-.058l.058-.069a.5.5 0 0 0-.058-.638L10.708 10l2.12-2.121l.058-.07a.5.5 0 0 0-.058-.637l-.069-.058a.5.5 0 0 0-.638.058L10 9.292l-2.121-2.12l-.07-.058z"
+                                Fill="#E81123"
+                                Visibility="Collapsed" />
+                            <Path
+                                Name="InfomationIcon"
+                                d:Visibility="Visible"
+                                Data="M18 10a8 8 0 1 0-16 0a8 8 0 0 0 16 0zM9.508 8.91a.5.5 0 0 1 .984 0L10.5 9v4.502l-.008.09a.5.5 0 0 1-.984 0l-.008-.09V9l.008-.09zM9.25 6.75a.75.75 0 1 1 1.5 0a.75.75 0 0 1-1.5 0z"
+                                Fill="SteelBlue"
+                                Visibility="Collapsed" />
+                            <Path
+                                Name="QuestionIcon"
+                                d:Visibility="Visible"
+                                Data="M10 2a8 8 0 1 1 0 16a8 8 0 0 1 0-16zm0 11.5a.75.75 0 1 0 0 1.5a.75.75 0 0 0 0-1.5zm0-8A2.5 2.5 0 0 0 7.5 8a.5.5 0 0 0 1 0a1.5 1.5 0 1 1 2.632.984l-.106.11l-.118.1l-.247.185a3.11 3.11 0 0 0-.356.323C9.793 10.248 9.5 10.988 9.5 12a.5.5 0 0 0 1 0c0-.758.196-1.254.535-1.614l.075-.076l.08-.073l.088-.072l.219-.163l.154-.125A2.5 2.5 0 0 0 10 5.5z"
+                                Fill="DarkCyan"
+                                Visibility="Collapsed" />
+                            <Path
+                                Name="WarningIcon"
+                                d:Visibility="Visible"
+                                Data="M8.686 2.852L2.127 14.777A1.5 1.5 0 0 0 3.441 17H16.56a1.5 1.5 0 0 0 1.314-2.223L11.314 2.852a1.5 1.5 0 0 0-2.628 0zM10 6.75a.75.75 0 0 1 .75.75v4a.75.75 0 0 1-1.5 0v-4a.75.75 0 0 1 .75-.75zm.75 7a.75.75 0 1 1-1.5 0a.75.75 0 0 1 1.5 0z"
+                                Fill="DarkOrange"
+                                Visibility="Collapsed" />
+                        </Grid>
+                    </Viewbox>
+                </StackPanel>
+                <Grid Grid.Column="1">
+                    <ScrollViewer Margin="0,20,0,0" VerticalScrollBarVisibility="Auto">
+                        <StackPanel Orientation="Vertical">
+                            <RichTextBox
+                                x:Name="MessageRichTextBox"
+                                Height="Auto"
+                                Margin="0,0,20,10"
+                                Background="Transparent"
+                                Block.LineHeight="20"
+                                BorderThickness="0"
+                                FontSize="15"
+                                Foreground="{Binding Color}"
+                                IsReadOnly="True"
+                                IsTabStop="False"
+                                IsUndoEnabled="False">
+                                <RichTextBox.Resources>
+                                    <Style TargetType="{x:Type Paragraph}">
+                                        <Setter Property="Margin" Value="0" />
+                                    </Style>
+                                </RichTextBox.Resources>
+                                <d:FlowDocument>
+                                    <Paragraph>
+                                        <Run>Message</Run>
+                                    </Paragraph>
+                                </d:FlowDocument>
+                            </RichTextBox>
+                            <Expander
+                                Margin="0,-6,20,0"
+                                HorizontalAlignment="Stretch"
+                                d:IsExpanded="True"
+                                d:Visibility="Visible"
+                                Background="Transparent"
+                                FlowDirection="RightToLeft">
+                                <TextBlock
+                                    Margin="0,0,0,0"
+                                    d:Text="Appended Message"
+                                    FlowDirection="LeftToRight"
+                                    FontSize="13"
+                                    Foreground="#3b3a39"
+                                    Text="{Binding AppendedMessage}"
+                                    TextWrapping="Wrap" />
+                                <Expander.Style>
+                                    <Style TargetType="Expander">
+                                        <Setter Property="Visibility" Value="Collapsed" />
+                                        <Style.Triggers>
+                                            <DataTrigger Binding="{Binding HasAppendedMessage}" Value="true">
+                                                <Setter Property="Visibility" Value="Visible" />
+                                            </DataTrigger>
+                                        </Style.Triggers>
+                                    </Style>
+                                </Expander.Style>
+                            </Expander>
+                            <StackPanel.ContextMenu>
+                                <ContextMenu>
+                                    <MenuItem Click="CopyMenuItem_Click" Header="Copy" />
+                                </ContextMenu>
+                            </StackPanel.ContextMenu>
+                        </StackPanel>
+                    </ScrollViewer>
+                </Grid>
+            </Grid>
+        </DockPanel>
+    </Border>
+</Window>

+ 376 - 0
UniformMaterialManagementSystem/Custom/MessageBoxEx.xaml.cs

@@ -0,0 +1,376 @@
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+
+namespace UniformMaterialManagementSystem.Custom
+{
+    public static class MessageEx
+    {
+        #region Information Dialog
+        public static MessageBoxResult ShowInformationDialog(string message)
+        {
+            return ShowInformationDialog(message, "");
+        }
+
+        public static MessageBoxResult ShowInformationDialog(string message, string appendedMessage)
+        {
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.OK, MessageBoxImage.Information);
+            cls.ShowDialog();
+            return cls.Result;
+        }
+
+        public static MessageBoxResult ShowInformationDialog(string message, Window owner)
+        {
+            return ShowInformationDialog(message, "", owner);
+        }
+
+        public static MessageBoxResult ShowInformationDialog(string message, string appendedMessage, Window owner)
+        {
+            owner.Opacity = 0.7;
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.OK, MessageBoxImage.Information, owner);
+            cls.ShowDialog();
+            owner.Opacity = 1;
+            return cls.Result;
+        }
+
+        public static MessageBoxResult ShowInformation(string message)
+        {
+            return ShowInformation(message, "");
+        }
+
+        public static MessageBoxResult ShowInformation(string message, string appendedMessage)
+        {
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.OK, MessageBoxImage.Information);
+            cls.Show();
+            return cls.Result;
+        }
+
+        #endregion
+
+        #region Question Dialog
+
+        public static MessageBoxResult ShowQuestionDialog(string message)
+        {
+            return ShowQuestionDialog(message, "");
+        }
+
+        public static MessageBoxResult ShowQuestionDialog(string message, string appendedMessage)
+        {
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.YesNo, MessageBoxImage.Question);
+            cls.ShowDialog();
+            return cls.Result;
+        }
+
+        public static MessageBoxResult ShowQuestionDialog(string message, Window owner)
+        {
+            return ShowQuestionDialog(message, "", owner);
+        }
+
+        public static MessageBoxResult ShowQuestionDialog(string message, string appendedMessage, Window owner)
+        {
+            owner.Opacity = 0.7;
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.YesNo, MessageBoxImage.Question, owner);
+            cls.ShowDialog();
+            owner.Opacity = 1;
+            return cls.Result;
+        }
+
+        #endregion
+
+        #region Warning Dialog
+
+        public static MessageBoxResult ShowWarningDialog(string message)
+        {
+            return ShowWarningDialog(message, "");
+        }
+
+        public static MessageBoxResult ShowWarningDialog(string message, string appendedMessage)
+        {
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.OK, MessageBoxImage.Warning);
+            cls.ShowDialog();
+            return cls.Result;
+        }
+
+        public static MessageBoxResult ShowWarningDialog(string message, Window owner)
+        {
+            return ShowWarningDialog(message, "", owner);
+        }
+
+        public static MessageBoxResult ShowWarningDialog(string message, string appendedMessage, Window owner)
+        {
+            owner.Opacity = 0.7;
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.OK, MessageBoxImage.Warning, owner);
+            cls.ShowDialog();
+            owner.Opacity = 1;
+            return cls.Result;
+        }
+
+        #endregion
+
+        #region Error Dialog
+
+        public static MessageBoxResult ShowErrorDialog(string message)
+        {
+            return ShowErrorDialog(message, "");
+        }
+
+        public static MessageBoxResult ShowErrorDialog(string message, string appendedMessage)
+        {
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.OK, MessageBoxImage.Error);
+            cls.ShowDialog();
+            return cls.Result;
+        }
+        public static MessageBoxResult ShowErrorDialog(string message, Window owner)
+        {
+            return ShowErrorDialog(message, "", owner);
+        }
+        public static MessageBoxResult ShowErrorDialog(string message, string appendedMessage, Window owner)
+        {
+            owner.Opacity = 0.7;
+            var cls = new MessageBoxEx(message, appendedMessage, MessageBoxButton.OK, MessageBoxImage.Error, owner);
+            cls.ShowDialog();
+            owner.Opacity = 1;
+            return cls.Result;
+        }
+
+        #endregion
+
+        #region General Dialog
+
+        public static MessageBoxResult Show(string message, string appendedMessage, MessageBoxButton messageBoxButton, MessageBoxImage messageBoxImage, Brush color)
+        {
+            var cls = new MessageBoxEx(message, appendedMessage, messageBoxButton, messageBoxImage, color);
+            cls.Show();
+            return cls.Result;
+        }
+
+        public static MessageBoxResult ShowDialog(string message, string appendedMessage, MessageBoxButton messageBoxButton, MessageBoxImage messageBoxImage, Brush color)
+        {
+            var cls = new MessageBoxEx(message, appendedMessage, messageBoxButton, messageBoxImage, color);
+            cls.ShowDialog();
+            return cls.Result;
+        }
+
+        public static MessageBoxResult ShowDialog(string message, string appendedMessage, MessageBoxButton messageBoxButton, MessageBoxImage messageBoxImage, Brush color, Window owner)
+        {
+            owner.Opacity = 0.7;
+            var cls = new MessageBoxEx(message, appendedMessage, messageBoxButton, messageBoxImage, color, owner);
+            cls.ShowDialog();
+            owner.Opacity = 1;
+            return cls.Result;
+        }
+
+        #endregion
+    }
+
+    public partial class MessageBoxEx : Window
+    {
+        #region Variables
+
+        public string Message { get; set; } = "";
+
+        public string AppendedMessage { get; set; } = "";
+
+        public bool HasAppendedMessage { get; private set; }
+
+        public MessageBoxImage Image { get; set; } = MessageBoxImage.Information;
+
+        public MessageBoxButton Button { get; set; } = MessageBoxButton.OK;
+
+        public MessageBoxResult Result { get; private set; } = MessageBoxResult.None;
+
+        public MessageBoxResult DefaultResult { get; set; } = MessageBoxResult.None;
+
+        public bool IsEnabledEffect { get; set; } = false;
+
+        #endregion
+
+        #region Caption Variables
+
+        public string OkCaption { get; set; } = "OK";
+
+        public string YesCaption { get; set; } = "Yes";
+
+        public string NoCaption { get; set; } = "No";
+
+        public string CancelCaption { get; set; } = "Cancel";
+
+        #endregion
+
+        #region Color Variables
+
+        public Brush Color { get; set; } = Brushes.Black;
+
+        public Brush BackgroundColor { get; set; } = Brushes.White;
+
+        #endregion
+
+        #region Initializer
+
+        public MessageBoxEx()
+        {
+            InitializeComponent();
+        }
+
+        public MessageBoxEx(string message, Window? owner = null)
+        {
+            InitializeComponent();
+            Message = message;
+            if (owner is not null && PresentationSource.FromVisual(owner) is not null) this.Owner = owner;
+        }
+        public MessageBoxEx(string message, string appendedMessage, Window? owner = null)
+        {
+            InitializeComponent();
+            Message = message;
+            AppendedMessage = appendedMessage;
+            if (owner is not null && PresentationSource.FromVisual(owner) is not null) this.Owner = owner;
+        }
+        public MessageBoxEx(string message, MessageBoxButton button, MessageBoxImage image, Window? owner = null)
+        {
+            InitializeComponent();
+            Message = message;
+            Button = button;
+            Image = image;
+            if (owner is not null && PresentationSource.FromVisual(owner) is not null) this.Owner = owner;
+        }
+
+        public MessageBoxEx(string message, string appendedMessage, MessageBoxButton button, MessageBoxImage image, Window? owner = null)
+        {
+            InitializeComponent();
+            Message = message;
+            AppendedMessage = appendedMessage;
+            Button = button;
+            Image = image;
+            if (owner is not null && PresentationSource.FromVisual(owner) is not null) this.Owner = owner;
+        }
+
+        public MessageBoxEx(string message, MessageBoxButton button, MessageBoxImage image, Brush color, Window? owner = null)
+        {
+            InitializeComponent();
+            Message = message;
+            Button = button;
+            Image = image;
+            Color = color;
+            if (owner is not null && PresentationSource.FromVisual(owner) is not null) this.Owner = owner;
+        }
+
+        public MessageBoxEx(string message, string appendedMessage, MessageBoxButton button, MessageBoxImage image, Brush color, Window? owner = null)
+        {
+            InitializeComponent();
+            Message = message;
+            AppendedMessage = appendedMessage;
+            Button = button;
+            Image = image;
+            Color = color;
+            if (owner is not null && PresentationSource.FromVisual(owner) is not null) this.Owner = owner;
+        }
+
+        #endregion
+
+        private void Window_Loaded(object sender, RoutedEventArgs e)
+        {
+            DataContext = this;
+            HasAppendedMessage = !string.IsNullOrEmpty(AppendedMessage);
+
+            var document = MessageRichTextBox.Document;
+            document.PagePadding = new Thickness(0);
+            var range = new TextRange(document.ContentStart, document.ContentEnd);
+            range.Text = Message;
+
+            SetupIconVisibility();
+            SetupButton();
+        }
+
+        private void CopyMenuItem_Click(object sender, RoutedEventArgs e)
+        {
+            if (sender is not MenuItem) return;
+
+            var s = Message + (string.IsNullOrEmpty(AppendedMessage) ? "" : "\r\n" + AppendedMessage);
+            Clipboard.SetText(s);
+        }
+
+        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
+        {
+            base.OnMouseLeftButtonDown(e);
+            this.DragMove();
+        }
+        private void SetupIconVisibility()
+        {
+            switch (Image)
+            {
+                case MessageBoxImage.None:
+                    break;
+                case MessageBoxImage.Error:  //Same : MessageBoxImage.Stop, Hand
+                    ErrorIcon.Visibility = Visibility.Visible;
+                    break;
+                case MessageBoxImage.Question:
+                    QuestionIcon.Visibility = Visibility.Visible;
+                    break;
+                case MessageBoxImage.Warning:  //Same : Exclamation
+                    WarningIcon.Visibility = Visibility.Visible;
+                    break;
+                case MessageBoxImage.Information:  //Same : Asterisk
+                    InfomationIcon.Visibility = Visibility.Visible;
+                    break;
+            }
+        }
+        private void SetupButton()
+        {
+            switch (Button)
+            {
+                case MessageBoxButton.OK:
+                    LeftButton.Visibility = Visibility.Collapsed;
+                    MiddleButton.Visibility = Visibility.Collapsed;
+                    RightButton.Content = OkCaption;
+                    RightButton.Tag = MessageBoxResult.OK; //Set MessageBoxResult to Tag
+                    Keyboard.Focus(RightButton);
+                    break;
+                case MessageBoxButton.OKCancel:
+                    LeftButton.Visibility = Visibility.Collapsed;
+                    MiddleButton.Content = OkCaption;
+                    RightButton.Content = CancelCaption;
+                    MiddleButton.Tag = MessageBoxResult.OK;
+                    RightButton.Tag = MessageBoxResult.Cancel;
+                    Keyboard.Focus(DefaultResult == MessageBoxResult.OK ? MiddleButton : RightButton);
+                    break;
+                case MessageBoxButton.YesNoCancel:
+                    LeftButton.Content = YesCaption;
+                    MiddleButton.Content = NoCaption;
+                    RightButton.Content = CancelCaption;
+                    LeftButton.Tag = MessageBoxResult.Yes;
+                    MiddleButton.Tag = MessageBoxResult.No;
+                    RightButton.Tag = MessageBoxResult.Cancel;
+                    switch (DefaultResult)
+                    {
+                        case MessageBoxResult.Yes:
+                            Keyboard.Focus(LeftButton);
+                            break;
+                        case MessageBoxResult.No:
+                            Keyboard.Focus(MiddleButton);
+                            break;
+                        default:
+                            Keyboard.Focus(RightButton);
+                            break;
+                    }
+                    break;
+                case MessageBoxButton.YesNo:
+                    LeftButton.Visibility = Visibility.Collapsed;
+                    MiddleButton.Content = YesCaption;
+                    RightButton.Content = NoCaption;
+                    MiddleButton.Tag = MessageBoxResult.Yes;
+                    RightButton.Tag = MessageBoxResult.No;
+                    Keyboard.Focus(DefaultResult == MessageBoxResult.Yes ? MiddleButton : RightButton);
+                    break;
+            }
+        }
+
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            var button = (Button)sender;
+            Result = (MessageBoxResult)button.Tag; //Get MessageBoxResult from Tag
+            this.Close();
+        }
+    }
+}

+ 8 - 4
UniformMaterialManagementSystem/ViewModels/RegisterPageViewModel.cs

@@ -4,6 +4,7 @@ using CommunityToolkit.Mvvm.Input;
 using System.ComponentModel.DataAnnotations;
 using System.Windows;
 using Microsoft.Extensions.DependencyInjection;
+using UniformMaterialManagementSystem.Custom;
 using UniformMaterialManagementSystem.Data;
 using UniformMaterialManagementSystem.Entities;
 using UniformMaterialManagementSystem.Services;
@@ -73,7 +74,7 @@ namespace UniformMaterialManagementSystem.ViewModels
         }
 
         [RelayCommand]
-        private async void CreateUser()
+        private async void CreateUser(Window window)
         {
             ValidateAllProperties();
 
@@ -100,13 +101,16 @@ namespace UniformMaterialManagementSystem.ViewModels
             await _service.InsertAsync(user);
             if (_service.SaveChanges())
             {
-                MessageBox.Show("创建成功,跳转到登录页面");
+                var result = MessageEx.ShowInformationDialog("创建成功,跳转到登录页面", window);
 
-                Login();
+                if (result == MessageBoxResult.OK)
+                {
+                    Login();
+                }
             }
             else
             {
-                MessageBox.Show("创建失败!请联系系统管理员");
+                MessageEx.ShowErrorDialog("创建失败!请联系系统管理员", window);
             }
         }