3
votes

I'm using the TextBoxValidationExtension in a MVVM pattern. I was having a problem with the validation because I'm setting my binding source in TwoWay mode in the NavigatedTo method which is called after the TextBoxFormatValidationHandler.Attach method is called. The first validation therefore occurred with empty value on the textbox which was applying the error styling to the textbox.

The binding in the NavigatedTo to the Text property of the textbox wasn't triggering a Textbox TextChanged event since from my comprehension the Textbox control is not loaded at this point.

So even tough I had a valid value binded to the textbox it appears to be invalid since the extensions didn't validated it.

     <TextBox Text="{Binding Path=ObjectXYZ.PropertyABC, Mode=TwoWay}"  
              extensions:TextBoxFocusExtensions.AutoSelectOnFocus="True"
              extensions:FieldValidationExtensions.Format="NonEmpty,Numeric">
1

1 Answers

2
votes

What I've done to solve the problem was to add to the WinRT Toolkit TextBoxFormatValidationHandler a handler to the loaded event of the textbox in the TextBoxFormatValidationHandler.Attach method:

 internal void Attach(TextBox textBox)
        {
            if (_textBox == textBox)
            {
                return;
            }

            if (_textBox != null)
            {
                this.Detach();
            }

            _textBox = textBox;
            _textBox.TextChanged += OnTextBoxTextChanged;
           _textBox.Loaded += _textBox_Loaded;
            this. Validate();
        }

        void _textBox_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            this.Validate();
        }

If someone has a better solution can you please let me know, thanks!