A helper class that provides various attached properties for the TextBox control.
Password watermarking code from: http://prabu-guru.blogspot.com/2010/06/how-to-add-watermark-text-to-textbox.html
예제 #1
0
        private static void TextBoxBaseContextMenuOpening(TextBoxBase sender, ContextMenuEventArgs e)
        {
            SpellingError spellingError;
            TextBoxBase   defaultTextBoxBaseContextMenu = (TextBoxBase)sender;
            TextBox       textBox     = defaultTextBoxBaseContextMenu as TextBox;
            RichTextBox   richTextBox = defaultTextBoxBaseContextMenu as RichTextBox;

            defaultTextBoxBaseContextMenu.ContextMenu = TextBoxHelper.GetDefaultTextBoxBaseContextMenu();
            int num = 0;

            if (textBox != null)
            {
                spellingError = textBox.GetSpellingError(textBox.CaretIndex);
            }
            else if (richTextBox != null)
            {
                spellingError = richTextBox.GetSpellingError(richTextBox.CaretPosition);
            }
            else
            {
                spellingError = null;
            }
            SpellingError spellingError1 = spellingError;

            if (spellingError1 != null)
            {
                IEnumerable <string> suggestions = spellingError1.Suggestions;
                if (suggestions.Any <string>())
                {
                    foreach (string suggestion in suggestions)
                    {
                        MenuItem menuItem = new MenuItem()
                        {
                            Header           = suggestion,
                            FontWeight       = FontWeights.Bold,
                            Command          = EditingCommands.CorrectSpellingError,
                            CommandParameter = suggestion,
                            CommandTarget    = defaultTextBoxBaseContextMenu
                        };
                        menuItem.SetResourceReference(FrameworkElement.StyleProperty, "MetroMenuItem");
                        defaultTextBoxBaseContextMenu.ContextMenu.Items.Insert(num, menuItem);
                        num++;
                    }
                    defaultTextBoxBaseContextMenu.ContextMenu.Items.Insert(num, new Separator());
                    num++;
                }
                MenuItem menuItem1 = new MenuItem()
                {
                    Header        = "Ignore All",
                    Command       = EditingCommands.IgnoreSpellingError,
                    CommandTarget = defaultTextBoxBaseContextMenu
                };
                menuItem1.SetResourceReference(FrameworkElement.StyleProperty, "MetroMenuItem");
                defaultTextBoxBaseContextMenu.ContextMenu.Items.Insert(num, menuItem1);
                num++;
                Separator separator = new Separator();
                defaultTextBoxBaseContextMenu.ContextMenu.Items.Insert(num, separator);
            }
        }
예제 #2
0
 private static void ControlGotFocus <TDependencyObject>(TDependencyObject sender, Action <TDependencyObject> action)
     where TDependencyObject : DependencyObject
 {
     if (sender != null && TextBoxHelper.GetSelectAllOnFocus(sender))
     {
         sender.Dispatcher.BeginInvoke(action, new object[] { sender });
     }
 }
예제 #3
0
 private static void OnNumericUpDownValueChaged(object sender, RoutedEventArgs e)
 {
     TextBoxHelper.SetTextLength <NumericUpDown>(sender as NumericUpDown, (NumericUpDown numericUpDown) => {
         if (!numericUpDown.Value.HasValue)
         {
             return(0);
         }
         return(1);
     });
 }
예제 #4
0
        public static void ClearControl(ExecutedRoutedEventArgs args)
        {
            if (args.Handled)
            {
                return;
            }

            if (!(args.OriginalSource is DependencyObject control) || false == TextBoxHelper.GetClearTextButton(control))
            {
                return;
            }

            switch (control)
            {
            case RichTextBox richTextBox:
                richTextBox.Document?.Blocks?.Clear();
                richTextBox.Selection?.Select(richTextBox.CaretPosition, richTextBox.CaretPosition);
                break;

            case DatePicker datePicker:
                datePicker.SetCurrentValue(DatePicker.SelectedDateProperty, null);
                datePicker.SetCurrentValue(DatePicker.TextProperty, null);
                datePicker.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
                break;

            case TimePickerBase timePicker:
                timePicker.SetCurrentValue(TimePickerBase.SelectedDateTimeProperty, null);
                timePicker.GetBindingExpression(TimePickerBase.SelectedDateTimeProperty)?.UpdateSource();
                break;

            case TextBox textBox:
                textBox.Clear();
                textBox.GetBindingExpression(TextBox.TextProperty)?.UpdateSource();
                break;

            case PasswordBox passwordBox:
                passwordBox.Clear();
                passwordBox.GetBindingExpression(PasswordBoxBindingBehavior.PasswordProperty)?.UpdateSource();
                break;

            case ComboBox comboBox:
            {
                if (comboBox.IsEditable)
                {
                    comboBox.SetCurrentValue(ComboBox.TextProperty, null);
                    comboBox.GetBindingExpression(ComboBox.TextProperty)?.UpdateSource();
                }

                comboBox.SetCurrentValue(ComboBox.SelectedItemProperty, null);
                comboBox.GetBindingExpression(ComboBox.SelectedItemProperty)?.UpdateSource();
                break;
            }
            }
        }
예제 #5
0
        private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TextBox)
            {
                TextBox textBox = d as TextBox;
                if (!(bool)e.NewValue)
                {
                    textBox.TextChanged -= new TextChangedEventHandler(TextBoxHelper.TextChanged);
                    textBox.GotFocus    -= new RoutedEventHandler(TextBoxHelper.TextBoxGotFocus);
                    return;
                }
                textBox.TextChanged += new TextChangedEventHandler(TextBoxHelper.TextChanged);
                textBox.GotFocus    += new RoutedEventHandler(TextBoxHelper.TextBoxGotFocus);
                textBox.Dispatcher.BeginInvoke(new Action(() => TextBoxHelper.TextChanged(textBox, new TextChangedEventArgs(TextBoxBase.TextChangedEvent, UndoAction.None))), new object[0]);
                return;
            }
            if (!(d is PasswordBox))
            {
                if (d is NumericUpDown)
                {
                    NumericUpDown newValue = d as NumericUpDown;
                    newValue.SelectAllOnFocus = (bool)e.NewValue;
                    if ((bool)e.NewValue)
                    {
                        newValue.ValueChanged += new RoutedPropertyChangedEventHandler <double?>(TextBoxHelper.OnNumericUpDownValueChaged);
                        newValue.GotFocus     += new RoutedEventHandler(TextBoxHelper.NumericUpDownGotFocus);
                        return;
                    }
                    newValue.ValueChanged -= new RoutedPropertyChangedEventHandler <double?>(TextBoxHelper.OnNumericUpDownValueChaged);
                    newValue.GotFocus     -= new RoutedEventHandler(TextBoxHelper.NumericUpDownGotFocus);
                }
                return;
            }
            PasswordBox passwordBox = d as PasswordBox;

            if (!(bool)e.NewValue)
            {
                passwordBox.PasswordChanged -= new RoutedEventHandler(TextBoxHelper.PasswordChanged);
                passwordBox.GotFocus        -= new RoutedEventHandler(TextBoxHelper.PasswordGotFocus);
                return;
            }
            passwordBox.PasswordChanged += new RoutedEventHandler(TextBoxHelper.PasswordChanged);
            passwordBox.GotFocus        += new RoutedEventHandler(TextBoxHelper.PasswordGotFocus);
            passwordBox.Dispatcher.BeginInvoke(new Action(() => TextBoxHelper.PasswordChanged(passwordBox, new RoutedEventArgs(PasswordBox.PasswordChangedEvent, passwordBox))), new object[0]);
        }
예제 #6
0
        public static void ButtonClicked(Button sender, RoutedEventArgs e)
        {
            DependencyObject parent = VisualTreeHelper.GetParent((Button)sender);

            while (!(parent is TextBox) && !(parent is PasswordBox) && !(parent is ComboBox))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            ICommand buttonCommand = TextBoxHelper.GetButtonCommand(parent);

            if (buttonCommand != null && buttonCommand.CanExecute(parent))
            {
                object   buttonCommandParameter = TextBoxHelper.GetButtonCommandParameter(parent);
                ICommand command = buttonCommand;
                object   obj     = buttonCommandParameter;
                if (obj == null)
                {
                    obj = parent;
                }
                command.Execute(obj);
            }
            if (TextBoxHelper.GetClearTextButton(parent))
            {
                if (parent is TextBox)
                {
                    ((TextBox)parent).Clear();
                    return;
                }
                if (parent is PasswordBox)
                {
                    ((PasswordBox)parent).Clear();
                    return;
                }
                if (parent is ComboBox)
                {
                    if (((ComboBox)parent).IsEditable)
                    {
                        ((ComboBox)parent).Text = string.Empty;
                    }
                    ((ComboBox)parent).SelectedItem = null;
                }
            }
        }
예제 #7
0
        private static void UseSpellCheckContextMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBoxBase defaultTextBoxBaseContextMenu = d as TextBoxBase;

            if (defaultTextBoxBaseContextMenu == null)
            {
                throw new InvalidOperationException("The property 'IsSpellCheckContextMenuEnabled' may only be set on TextBoxBase elements.");
            }
            if ((bool)e.NewValue)
            {
                defaultTextBoxBaseContextMenu.SetValue(SpellCheck.IsEnabledProperty, (object)true);
                defaultTextBoxBaseContextMenu.ContextMenu         = TextBoxHelper.GetDefaultTextBoxBaseContextMenu();
                defaultTextBoxBaseContextMenu.ContextMenuOpening += new ContextMenuEventHandler(TextBoxHelper.TextBoxBaseContextMenuOpening);
                return;
            }
            defaultTextBoxBaseContextMenu.SetValue(SpellCheck.IsEnabledProperty, (object)false);
            defaultTextBoxBaseContextMenu.ContextMenu         = TextBoxHelper.GetDefaultTextBoxBaseContextMenu();
            defaultTextBoxBaseContextMenu.ContextMenuOpening -= new ContextMenuEventHandler(TextBoxHelper.TextBoxBaseContextMenuOpening);
        }
예제 #8
0
        private static void CanClearControl(CanExecuteRoutedEventArgs args)
        {
            if (args.Handled)
            {
                return;
            }

            if (args.OriginalSource is not DependencyObject control || false == TextBoxHelper.GetClearTextButton(control))
            {
                return;
            }

            args.CanExecute = control switch
            {
                DatePicker datePicker => !ControlsHelper.GetIsReadOnly(datePicker),
                TimePickerBase timePicker => !timePicker.IsReadOnly,
                TextBoxBase textBox => !textBox.IsReadOnly,
                ComboBox comboBox => !comboBox.IsReadOnly,
                _ => true
            };
        }
예제 #9
0
        private static void ButtonCommandOrClearTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBox textBox = d as TextBox;

            if (textBox != null)
            {
                textBox.Loaded -= new RoutedEventHandler(TextBoxHelper.TextChanged);
                textBox.Loaded += new RoutedEventHandler(TextBoxHelper.TextChanged);
                if (textBox.IsLoaded)
                {
                    TextBoxHelper.TextChanged(textBox, new RoutedEventArgs());
                }
            }
            PasswordBox passwordBox = d as PasswordBox;

            if (passwordBox != null)
            {
                passwordBox.Loaded -= new RoutedEventHandler(TextBoxHelper.PasswordChanged);
                passwordBox.Loaded += new RoutedEventHandler(TextBoxHelper.PasswordChanged);
                if (passwordBox.IsLoaded)
                {
                    TextBoxHelper.PasswordChanged(passwordBox, new RoutedEventArgs());
                }
            }
            ComboBox comboBox = d as ComboBox;

            if (comboBox != null)
            {
                comboBox.Loaded -= new RoutedEventHandler(TextBoxHelper.ComboBoxLoaded);
                comboBox.Loaded += new RoutedEventHandler(TextBoxHelper.ComboBoxLoaded);
                if (comboBox.IsLoaded)
                {
                    TextBoxHelper.ComboBoxLoaded(comboBox, new RoutedEventArgs());
                }
            }
        }
예제 #10
0
 public static void SetButtonWidth(DependencyObject obj, double value)
 {
     TextBoxHelper.SetButtonWidth(obj, value);
 }
예제 #11
0
 public static double GetButtonWidth(DependencyObject obj)
 {
     return(TextBoxHelper.GetButtonWidth(obj));
 }
예제 #12
0
 private static void TextChanged(object sender, RoutedEventArgs e)
 {
     TextBoxHelper.SetTextLength <TextBox>(sender as TextBox, (TextBox textBox) => textBox.Text.Length);
 }
예제 #13
0
 private static void TextBoxGotFocus(object sender, RoutedEventArgs e)
 {
     TextBoxHelper.ControlGotFocus <TextBox>(sender as TextBox, (TextBox textBox) => textBox.SelectAll());
 }
예제 #14
0
 private static void PasswordGotFocus(object sender, RoutedEventArgs e)
 {
     TextBoxHelper.ControlGotFocus <PasswordBox>(sender as PasswordBox, (PasswordBox passwordBox) => passwordBox.SelectAll());
 }
예제 #15
0
 private static void PasswordChanged(object sender, RoutedEventArgs e)
 {
     TextBoxHelper.SetTextLength <PasswordBox>(sender as PasswordBox, (PasswordBox passwordBox) => passwordBox.Password.Length);
 }
예제 #16
0
 private static void NumericUpDownGotFocus(object sender, RoutedEventArgs e)
 {
     TextBoxHelper.ControlGotFocus <NumericUpDown>(sender as NumericUpDown, (NumericUpDown numericUpDown) => numericUpDown.SelectAll());
 }