Wednesday, December 9, 2009

Silverlight 3 : Enable and Disable Validation For Control.

Please check previous post about validation with silverlight.

Enable and Disable Validation or you can say manually adding and remove binding for control.

In previous post i bound myName TextBox to Name property of Customer, But some case you do not want to validate that control and to do so you need to clear binding. ( Simplest way as per my thinking).

1. Remove Binding in Silverlight or Disable Validation

// Clear all errors from validation summary. ( Review previous post XAML)

valida.Errors.Clear();

// Set Text to clear ( As per your need )
myName.Text = String.Empty;

//This call will cleae binding for TextProperty( Take any dependency property)
myName.SetValue(TextBox.TextProperty, DependencyProperty.UnsetValue);
            myName.ClearValue(TextBox.TextProperty);

2. Adding Binding in Silverlight or Enable Validation.

Binding binding = new Binding("Name");
           binding.ValidatesOnExceptions = true;
           binding.NotifyOnValidationError = true;
           binding.Mode = BindingMode.TwoWay;
           myName.SetBinding(TextBox.TextProperty, binding);


Let me know your comment on this.

1 comment:

Gauri said...

I understand clearing the errors. Though in case a textbox whose validation has been failed is disabled on some other operation, how to disregard that validation persisting the earlier value entered by user?