private Prompt(string title, string description, string defaultValue, string placeholder, string toolTip, bool multiline, bool passwordMode, bool required, int maxLength, IEnumerable <string> suggestions, bool suggestionsFixed, string comment) { DataContext = new ViewModel(description, defaultValue, placeholder, toolTip, required); InitializeComponent(); Buttons = new[] { OkButton, CancelButton }; _multiline = multiline; if (required) { OkButton.SetBinding(IsEnabledProperty, new Binding { Source = DataContext, Path = new PropertyPath(nameof(ViewModel.Text)), Converter = this }); } if (comment != null) { ButtonsRowContent = new BbCodeBlock { Text = comment, Style = (Style)FindResource(@"BbCodeBlock.Small") }; ButtonsRowContentAlignment = HorizontalAlignment.Left; } 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 = !suggestionsFixed, IsTextSearchEnabled = true }; if (suggestions is INotifyCollectionChanged) { comboBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = suggestions }); } else { comboBox.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 }); if (comboBox.Template?.FindName(@"PART_EditableTextBox", comboBox) is TextBox textBox) { textBox.SelectAll(); textBox.Focus(); } else { comboBox.Focus(); } if (placeholder != null) { comboBox.Placeholder = placeholder; } element = comboBox; } else { var textBox = new BetterTextBox(); textBox.SetBinding(TextBox.TextProperty, new Binding { Source = DataContext, Path = new PropertyPath(nameof(ViewModel.Text)), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, ValidatesOnDataErrors = true }); if (maxLength != -1) { textBox.MaxLength = maxLength; } if (multiline) { textBox.AcceptsReturn = true; textBox.TextWrapping = TextWrapping.Wrap; textBox.Height = 240; } if (placeholder != null) { textBox.Placeholder = placeholder; } textBox.Focus(); textBox.SelectAll(); element = textBox; } Panel.Children.Add(element); if (toolTip != null) { element.SetValue(ToolTipProperty, toolTip); } PreviewKeyDown += OnKeyDown; element.PreviewKeyDown += OnKeyDown; Closing += (sender, args) => { if (MessageBoxResult != MessageBoxResult.OK) { return; } Result = Model.Text; }; }
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; }; }
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; } 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; }; }