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 placeholder, string toolTip, bool multiline, bool passwordMode, bool required, int maxLength, IEnumerable <string> suggestions, bool suggestionsFixed, string comment, Func <string, Task <IEnumerable <string> > > suggestionsCallback = null, Func <string, Task <string> > verificationCallback = null, bool suggestionsAsList = false) { DataContext = new ViewModel(description, defaultValue, placeholder, toolTip, required) { SuggestionsCallback = suggestionsCallback, VerificationCallback = verificationCallback, }; if (Model.SuggestionsCallback != null) { Model.UpdateSuggestionsAsync().Ignore(); } InitializeComponent(); Buttons = new[] { OkButton, CancelButton }; _multiline = multiline; var suggestionsList = new Lazy <List <string> >(suggestions.ToList); 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 }); element = passwordBox; } else if (suggestions != null || suggestionsCallback != null) { var comboBox = new BetterComboBox { IsEditable = !suggestionsFixed, IsTextSearchEnabled = true }; if (suggestions is INotifyCollectionChanged) { comboBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = suggestions }); } else if (suggestions != null) { comboBox.ItemsSource = suggestionsList.Value; } else { Model.SuggestionsDynamic = new BetterObservableCollection <string>(); comboBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = Model.SuggestionsDynamic }); } comboBox.SetBinding(ComboBox.TextProperty, new Binding { Source = DataContext, Path = new PropertyPath(nameof(ViewModel.Text)), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, ValidatesOnDataErrors = true }); 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; } element = textBox; } Panel.Children.Add(element); if (suggestionsAsList) { var listBox = new ListBox { IsTextSearchEnabled = true, Height = 200 }; if (suggestions is INotifyCollectionChanged) { listBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = suggestions }); } else if (suggestions != null) { listBox.ItemsSource = suggestionsList.Value; } else { if (Model.SuggestionsDynamic == null) { Model.SuggestionsDynamic = new BetterObservableCollection <string>(); } listBox.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { Source = Model.SuggestionsDynamic }); } listBox.SetBinding(Selector.SelectedItemProperty, new Binding { Source = DataContext, Path = new PropertyPath(nameof(ViewModel.TextInList)), Mode = BindingMode.TwoWay }); Panel.Children.Add(listBox); } if (toolTip != null) { element.SetValue(ToolTipProperty, toolTip); } PreviewKeyDown += OnKeyDown; element.PreviewKeyDown += OnKeyDown; _element = element; 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; }; }