3

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">
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100

1 Answers1

2

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!

  • Interesting, indeed it seems like the TextChanged event doesn't get raised when the Text value gets changed before the control is loaded. I wonder if it is a bug in the platform. Thanks. I'll apply your suggested fix to the toolkit. – Filip Skakun May 11 '13 at 06:15
  • 1
    Make sure to get the latest version. – Filip Skakun May 11 '13 at 06:20