DirectoryCreator/InputDialog.cs

102 lines
3.1 KiB
C#

using System.Windows;
using System.Windows.Controls;
namespace Microsoft.Windows.Controls
{
public class InputDialogResult
{
public bool IsOk { get; set; }
public string Result { get; set; }
}
public class InputDialog
{
public static InputDialogResult Show(string title, string promptText, string defaultValue = "")
{
var dialog = new Window
{
Title = title,
Width = 400,
Height = 200, // Aumentado para dar más espacio
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = Application.Current.MainWindow,
ResizeMode = ResizeMode.NoResize
};
var grid = new Grid { Margin = new Thickness(10) };
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); // Prompt
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(10) }); // Spacing
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); // TextBox
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(20) }); // Spacing
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); // Buttons
var promptLabel = new TextBlock
{
Text = promptText,
TextWrapping = TextWrapping.Wrap,
Margin = new Thickness(0, 0, 0, 5)
};
Grid.SetRow(promptLabel, 0);
var inputBox = new TextBox
{
Text = defaultValue,
Height = 23
};
Grid.SetRow(inputBox, 2);
var buttonPanel = new StackPanel
{
Orientation = Orientation.Horizontal,
HorizontalAlignment = HorizontalAlignment.Right
};
Grid.SetRow(buttonPanel, 4);
var okButton = new Button
{
Content = "Aceptar",
Width = 75,
Height = 23,
Margin = new Thickness(0, 0, 10, 0),
IsDefault = true
};
var cancelButton = new Button
{
Content = "Cancelar",
Width = 75,
Height = 23,
IsCancel = true
};
buttonPanel.Children.Add(okButton);
buttonPanel.Children.Add(cancelButton);
grid.Children.Add(promptLabel);
grid.Children.Add(inputBox);
grid.Children.Add(buttonPanel);
dialog.Content = grid;
var result = new InputDialogResult();
okButton.Click += (s, e) =>
{
result.Result = inputBox.Text;
result.IsOk = true;
dialog.Close();
};
cancelButton.Click += (s, e) =>
{
result.IsOk = false;
dialog.Close();
};
inputBox.Focus();
dialog.ShowDialog();
return result;
}
}
}