-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenameDialog.xaml.cs
More file actions
62 lines (51 loc) · 1.43 KB
/
Copy pathRenameDialog.xaml.cs
File metadata and controls
62 lines (51 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Windows;
using System.Windows.Input;
namespace KnowledgeBaseViewer;
public partial class RenameDialog : Window
{
public string NewName { get; private set; } = string.Empty;
public RenameDialog(string currentName)
{
InitializeComponent();
NameTextBox.Text = currentName;
Loaded += (_, _) =>
{
NameTextBox.Focus();
NameTextBox.SelectAll();
};
}
private void NameTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
TryAccept();
}
else if (e.Key == Key.Escape)
{
DialogResult = false;
}
}
private void RenameButton_Click(object sender, RoutedEventArgs e) => TryAccept();
private void CancelButton_Click(object sender, RoutedEventArgs e) => DialogResult = false;
private void TryAccept()
{
var name = NameTextBox.Text.Trim();
if (string.IsNullOrWhiteSpace(name))
{
ShowError("Name cannot be empty.");
return;
}
if (name.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) >= 0)
{
ShowError("Name contains invalid characters.");
return;
}
NewName = name;
DialogResult = true;
}
private void ShowError(string message)
{
ErrorText.Text = message;
ErrorText.Visibility = Visibility.Visible;
}
}