NumericTextBoxBehavior Suggestions

Jan 20, 2010 at 3:17 PM

Hi,

I wanted to use your NumericTextBoxBehavior class with an editable combobox and I had to make some minor changes in the class in order for it to work.

I changed the OnEnableStateChanged method so it can handle more than just text boxes.

 

        private static void OnEnabledStateChanged(DependencyObject sender,
            DependencyPropertyChangedEventArgs e)
        {
            UIElement tb = sender as UIElement;

            if (tb == null || !(sender is ComboBox || sender is TextBox))
                return;
// etcetera.....

 

The first part of tbb_PreviewTextInput and OnClipboardPaste changes to:

 

        static void tbb_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox tb = getEditableTextBox(sender);

where getEditableTextBox looks like:

        private static TextBox getEditableTextBox(object sender)
        {
            Control ctrl = sender as Control;
            return ((sender is ComboBox) ? ctrl.Template.FindName("PART_EditableTextBox", ctrl) :
                ctrl) as TextBox;
        }

I also changed the regular expression so it could handle scientific notation:

string expr = @"^([-+]?)(\d*)([,.]?)(\d*)([eE]?)([+-]?)\d*$";

Maybe you want to implement it also. If not no problem.

Thanks for this great framework

Robert

May 4, 2010 at 9:22 AM
Robert Thanks for this code, though I don't think I will include this in the Textbox behaviour, as I want to keep it just for TextBoxes.