This is a migrated thread and some comments may be shown as answers.

[Solved] Set validationState in Behavior

2 Answers 126 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Matej
Top achievements
Rank 1
Matej asked on 15 Jul 2014, 02:47 PM
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 Answers, 1 is accepted

Sort by
0
Matej
Top achievements
Rank 1
answered on 15 Jul 2014, 02:57 PM
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)
0
Tsvyatko
Telerik team
answered on 18 Jul 2014, 10:46 AM
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.

 
Tags
TextBox
Asked by
Matej
Top achievements
Rank 1
Answers by
Matej
Top achievements
Rank 1
Tsvyatko
Telerik team
Share this question
or