private Prompt(string title, string description, string defaultValue, string watermark, string toolTip, bool multiline, bool passwordMode, bool required,
int maxLength, IEnumerable<string> suggestions) {
DataContext = new ViewModel(description, defaultValue, watermark, toolTip, required);
InitializeComponent();
Buttons = new[] { OkButton, CancelButton };
if (required) {
OkButton.SetBinding(IsEnabledProperty, new Binding {
Source = DataContext,
Path = new PropertyPath(nameof(ViewModel.Text)),
Converter = this
});
}
Title = title;
FrameworkElement element;
if (passwordMode) {
var passwordBox = new ProperPasswordBox();
passwordBox.SetBinding(ProperPasswordBox.PasswordProperty, new Binding {
Source = DataContext,
Path = new PropertyPath(nameof(ViewModel.Text)),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
passwordBox.Focus();
passwordBox.SelectAll();
element = passwordBox;
} else if (suggestions != null) {
var comboBox = new BetterComboBox {
IsEditable = true,
IsTextSearchEnabled = true,
ItemsSource = suggestions.ToList()
};
comboBox.SetBinding(ComboBox.TextProperty, new Binding {
Source = DataContext,
Path = new PropertyPath(nameof(ViewModel.Text)),
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
ValidatesOnDataErrors = true
});
var textBox = comboBox.Template?.FindName(@"PART_EditableTextBox", comboBox) as TextBox;
if (textBox != null) {
textBox.SelectAll();
textBox.Focus();
} else {
comboBox.Focus();
}
if (watermark != null) {
comboBox.Placeholder = watermark;
}
element = comboBox;
} else {
var textBox = new BetterTextBox();
textBox.SetBinding(TextBox.TextProperty, new Binding {
Source = DataContext,
Path = new PropertyPath(nameof(ViewModel.Text)),
Mode = BindingMode.TwoWay
});
if (maxLength != -1) {
textBox.MaxLength = maxLength;
}
if (multiline) {
textBox.AcceptsReturn = true;
textBox.TextWrapping = TextWrapping.Wrap;
textBox.Height = 240;
textBox.Width = 480;
}
if (watermark != null) {
textBox.Placeholder = watermark;
}
textBox.Focus();
textBox.SelectAll();
element = textBox;
}
Panel.Children.Add(element);
if (toolTip != null) element.SetValue(ToolTipProperty, toolTip);
Closing += (sender, args) => {
if (MessageBoxResult != MessageBoxResult.OK) return;
Result = Model.Text;
};
}