GetClearTextButton() public static method

public static GetClearTextButton ( DependencyObject d ) : bool
d System.Windows.DependencyObject
return bool
Exemplo n.º 1
0
        private static void CanClearControl(CanExecuteRoutedEventArgs args)
        {
            if (args.Handled)
            {
                return;
            }

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

            args.CanExecute = true;

            switch (control)
            {
            case DatePicker datePicker:
                args.CanExecute = !ControlsHelper.GetIsReadOnly(datePicker);
                break;

            case TimePickerBase timePicker:
                args.CanExecute = !timePicker.IsReadOnly;
                break;

            case TextBoxBase textBox:
                args.CanExecute = !textBox.IsReadOnly;
                break;

            case ComboBox comboBox:
                args.CanExecute = !comboBox.IsReadOnly;
                break;
            }
        }
Exemplo n.º 2
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;
            }
            }
        }
Exemplo n.º 3
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;
                }
            }
        }
Exemplo n.º 4
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
            };
        }