Set validationState in Behavior

3 posts, 0 answers
  1. Matej
    Matej avatar
    3 posts
    Member since:
    Nov 2013

    Posted 15 Jul 2014 Link to this post

    Hi

    I am trying set validation state with custom Behavior:
    My behaviour is:

    public class ValidationBehavior : Behavior<RadTextBox>
       {
           public String ValidationError
           {
               get { return (String)GetValue(ValidationErrorProperty); }
               set { SetValue(ValidationErrorProperty, value); }
           }
     
           // Using a DependencyProperty as the backing store for ValidationError.  This enables animation, styling, binding, etc...
           public static readonly DependencyProperty ValidationErrorProperty =
               DependencyProperty.Register("ValidationError", typeof(String), typeof(ValidationBehavior), new PropertyMetadata(null, validationErrorChanged));
     
           private static void validationErrorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
           {
               if ((d as ValidationBehavior).AssociatedObject != null)
               {
                   (d as ValidationBehavior).UpdateValidationError();
               }
           }
     
           protected override void OnAttached()
           {
               base.OnAttached();
     
               UpdateValidationError();
           }
     
           void UpdateValidationError()
           {
               if (ValidationError == null)
                   AssociatedObject.ChangeValidationState(ValidationState.NotValidated, "");
               else
                   AssociatedObject.ChangeValidationState(ValidationState.Invalid, ValidationError);
           }
       }


    My xaml is:

    <StackPanel
          x:Name="ContentPanel"
          Grid.Row="1"
          Margin="12,0,12,0">
     
          <telerikPrimitives:RadTextBox
              x:Name="text1" />
          <telerikPrimitives:RadTextBox
              x:Name="text2">
              <i:Interaction.Behaviors>
                  <local:ValidationBehavior
                      ValidationError="Error" />
              </i:Interaction.Behaviors>
     
          </telerikPrimitives:RadTextBox>
     
      </StackPanel>

    To textbox1 I set validationState in codebehind, textbox2 has set throw behavior. But result is different, see screenshots.
    No error messages showed, but focus state is correct.


  2. Matej
    Matej avatar
    3 posts
    Member since:
    Nov 2013

    Posted 15 Jul 2014 Link to this post

    Is this bug in your control or in my code? (it is WP8.0 Silverlight project and controls are in version 2014.2.617.3040)
  3. DevCraft banner
  4. Tsvyatko
    Admin
    Tsvyatko avatar
    833 posts

    Posted 18 Jul 2014 Link to this post

    Hello Matej,

    it seems that using beahvior results in scenario in which ValidationState is set before the item is loaded. To avoid that you can modify OnAttached method in the following way:
    protected override void OnAttached()
    {
        base.OnAttached();
     
        this.AssociatedObject.Loaded += (s, e) => this.UpdateValidationError();
    }

    As this does not seems to be expected behavior we will log this as bug and look for solution to support the scenario without the code proposed.

    Thank you for bringing your attention into this. I have updated your telerik points accordingly.

    Regards,
    Tsvyatko
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top