瀏覽代碼

Added images to new messagebox

Kenric Nugteren 1 年之前
父節點
當前提交
2c2a578a45
共有 2 個文件被更改,包括 47 次插入11 次删除
  1. 19 3
      inabox.wpf/Forms/MessageWindow.xaml
  2. 28 8
      inabox.wpf/Forms/MessageWindow.xaml.cs

+ 19 - 3
inabox.wpf/Forms/MessageWindow.xaml

@@ -38,9 +38,25 @@
         </Grid.RowDefinitions>
         <Border Grid.Row="0" BorderBrush="LightGray" BorderThickness="1"
                 Margin="2" Padding="5" Background="White">
-            <TextBlock x:Name="MessageBox" Text="{Binding Message}"
-                       TextWrapping="Wrap"
-                       Margin="5" VerticalAlignment="Center"/>
+            <DockPanel>
+                <Image x:Name="ImageControl" Margin="5" Source="{Binding Image}"
+                       DockPanel.Dock="Left" MaxWidth="50" MaxHeight="50">
+                    <Image.Style>
+                        <Style TargetType="Image">
+                            <Setter Property="Visibility" Value="Visible"/>
+                            <Style.Triggers>
+                                <DataTrigger Binding="{Binding Image}" Value="{x:Null}">
+                                    <Setter Property="Visibility" Value="Collapsed"/>
+                                </DataTrigger>
+                            </Style.Triggers>
+                        </Style>
+                    </Image.Style>
+                </Image>
+                <TextBlock x:Name="MessageBox" Text="{Binding Message}"
+                           TextWrapping="Wrap"
+                           Margin="5" VerticalAlignment="Center"
+                           DockPanel.Dock="Right"/>
+            </DockPanel>
         </Border>
         <Border Grid.Row="1"
                 Background="WhiteSmoke" BorderBrush="Gray" BorderThickness="1" Margin="2,0,2,2">

+ 28 - 8
inabox.wpf/Forms/MessageWindow.xaml.cs

@@ -10,6 +10,9 @@ using System.Runtime.CompilerServices;
 using System.Windows;
 using System.Windows.Controls;
 using System.IO;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using InABox.WPF;
 
 namespace InABox.Wpf;
 
@@ -83,6 +86,17 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
         }
     }
 
+    public ImageSource? _image = null;
+    public ImageSource? Image
+    {
+        get => _image;
+        set
+        {
+            _image = value;
+            OnPropertyChanged();
+        }
+    }
+
     private string _details = "";
 
     public string Details
@@ -163,23 +177,27 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
 
     #region Static Constructors
 
-    public static void ShowMessage(string message, string title)
+    public static readonly BitmapImage _warning = InABox.Wpf.Resources.warning.AsBitmapImage();
+
+    public static void ShowMessage(string message, string title, ImageSource? image = null)
     {
         var window = new MessageWindow
         {
             Message = message,
-            Title = title
+            Title = title,
+            Image = image
         };
         window.AddOKButton();
         window.ShowDialog();
     }
 
-    public static bool ShowOKCancel(string message, string title)
+    public static bool ShowOKCancel(string message, string title, ImageSource? image = null)
     {
         var window = new MessageWindow
         {
             Message = message,
-            Title = title
+            Title = title,
+            Image = image
         };
         window.AddOKButton();
         window.AddCancelButton();
@@ -187,12 +205,13 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
         return window.Result == MessageWindowResult.OK;
     }
 
-    public static bool ShowYesNo(string message, string title)
+    public static bool ShowYesNo(string message, string title, ImageSource? image = null)
     {
         var window = new MessageWindow
         {
             Message = message,
-            Title = title
+            Title = title,
+            Image = image
         };
         window.AddOKButton("Yes");
         window.AddCancelButton("No");
@@ -207,7 +226,7 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
     /// <param name="exception"></param>
     /// <param name="title"></param>
     /// <param name="shouldLog">If <see langword="true"/>, also logs the exception.</param>
-    public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true)
+    public static void ShowError(string? message, Exception exception, string title = "Error", bool shouldLog = true, ImageSource? image = null)
     {
         if (shouldLog)
         {
@@ -218,7 +237,8 @@ public partial class MessageWindow : Window, INotifyPropertyChanged
         {
             Message = message ?? exception.Message,
             Title = title,
-            Details = CoreUtils.FormatException(exception)
+            Details = CoreUtils.FormatException(exception),
+            Image = image ?? _warning
         };
 
         window.AddButton(new MessageWindowButton("Show Logs", ShowLogs_Click, MessageWindowButtonPosition.Left));