diff --git a/src/Avalonia.Controls/SystemDialog.cs b/src/Avalonia.Controls/SystemDialog.cs
index 4d217d7459..4a9e745e30 100644
--- a/src/Avalonia.Controls/SystemDialog.cs
+++ b/src/Avalonia.Controls/SystemDialog.cs
@@ -52,6 +52,11 @@ namespace Avalonia.Controls
///
public string? DefaultExtension { get; set; }
+ ///
+ /// Gets or sets a value indicating whether to display a warning if the user specifies the name of a file that already exists.
+ ///
+ public bool? ShowOverwritePrompt { get; set; }
+
///
/// Shows the save file dialog.
///
diff --git a/src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs b/src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs
index 28d40f13b9..405a248caf 100644
--- a/src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs
+++ b/src/Avalonia.Dialogs/ManagedFileChooserViewModel.cs
@@ -17,6 +17,7 @@ namespace Avalonia.Dialogs
private readonly ManagedFileDialogOptions _options;
public event Action CancelRequested;
public event Action CompleteRequested;
+ public event Action OverwritePrompt;
public AvaloniaList QuickLinks { get; } =
new AvaloniaList();
@@ -39,6 +40,7 @@ namespace Avalonia.Dialogs
private bool _scheduledSelectionValidation;
private bool _alreadyCancelled = false;
private string _defaultExtension;
+ private bool _overwritePrompt;
private CompositeDisposable _disposables;
public string Location
@@ -167,6 +169,7 @@ namespace Avalonia.Dialogs
{
_savingFile = true;
_defaultExtension = sfd.DefaultExtension;
+ _overwritePrompt = sfd.ShowOverwritePrompt ?? true;
FileName = sfd.InitialFileName;
}
@@ -360,7 +363,16 @@ namespace Avalonia.Dialogs
FileName = Path.ChangeExtension(FileName, _defaultExtension);
}
- CompleteRequested?.Invoke(new[] { Path.Combine(Location, FileName) });
+ var fullName = Path.Combine(Location, FileName);
+
+ if (_overwritePrompt && File.Exists(fullName))
+ {
+ OverwritePrompt?.Invoke(fullName);
+ }
+ else
+ {
+ CompleteRequested?.Invoke(new[] { fullName });
+ }
}
}
else
diff --git a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs
index f9e62d905b..1970c5557d 100644
--- a/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs
+++ b/src/Avalonia.Dialogs/ManagedFileDialogExtensions.cs
@@ -1,3 +1,4 @@
+using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Avalonia.Controls;
@@ -31,6 +32,75 @@ namespace Avalonia.Dialogs
dialog.Close();
};
+ model.OverwritePrompt += async (filename) =>
+ {
+ Window overwritePromptDialog = new Window()
+ {
+ Title = "Confirm Save As",
+ SizeToContent = SizeToContent.WidthAndHeight,
+ WindowStartupLocation = WindowStartupLocation.CenterOwner,
+ Padding = new Thickness(10),
+ MinWidth = 270
+ };
+
+ string name = Path.GetFileName(filename);
+
+ var panel = new DockPanel()
+ {
+ HorizontalAlignment = Layout.HorizontalAlignment.Stretch
+ };
+
+ var label = new Label()
+ {
+ Content = $"{name} already exists.\nDo you want to replace it?"
+ };
+
+ panel.Children.Add(label);
+ DockPanel.SetDock(label, Dock.Top);
+
+ var buttonPanel = new StackPanel()
+ {
+ HorizontalAlignment = Layout.HorizontalAlignment.Right,
+ Orientation = Layout.Orientation.Horizontal,
+ Spacing = 10
+ };
+
+ var button = new Button()
+ {
+ Content = "Yes",
+ HorizontalAlignment = Layout.HorizontalAlignment.Right
+ };
+
+ button.Click += (sender, args) =>
+ {
+ result = new string[1] { filename };
+ overwritePromptDialog.Close();
+ dialog.Close();
+ };
+
+ buttonPanel.Children.Add(button);
+
+ button = new Button()
+ {
+ Content = "No",
+ HorizontalAlignment = Layout.HorizontalAlignment.Right
+ };
+
+ button.Click += (sender, args) =>
+ {
+ overwritePromptDialog.Close();
+ };
+
+ buttonPanel.Children.Add(button);
+
+ panel.Children.Add(buttonPanel);
+ DockPanel.SetDock(buttonPanel, Dock.Bottom);
+
+ overwritePromptDialog.Content = panel;
+
+ await overwritePromptDialog.ShowDialog(dialog);
+ };
+
model.CancelRequested += dialog.Close;
await dialog.ShowDialog