views:

8846

answers:

4

How have you decided to handle data/control validation in your silverlight applications?

A: 

Haven't done much of this yet personally, but some good starting points here. I imagine that these will come as part of a future release, but for now we'll probably have to roll our own using ASP.NET validators as a starting point.

Bryant
+4  A: 

You can throw and capture data validation exceptions.

To manage both of these types of errors need to take 3 steps:

  1. Identify the error handler either in the control or higher in the visiblity hierarchy (e.g., a container; in this case the grid that contains the text box)
  2. Set NotifyOnValidationError and ValidateOnException to true. The latter tells the Binding Engine to create a validation error event when an exception occurs. The former tells the Binding Engine to raise the BindingValidationError event when a validation error occurs.
  3. Create the event handler named in step 1.

Taken from here.

Sample code:

// page.xaml.cs

private bool clean = true;


private void LayoutRoot_BindingValidationError( 
   object sender, ValidationErrorEventArgs e )
{
   if ( e.Action == ValidationErrorEventAction.Added )
   {
      QuantityOnHand.Background = new SolidColorBrush( Colors.Red );
      clean = false;
   }
   else if ( e.Action == ValidationErrorEventAction.Removed )
   {
      QuantityOnHand.Background = new SolidColorBrush( Colors.White );
      clean = true;
   }
}



// page.xaml

<Grid x:Name="LayoutRoot" Background="White" BindingValidationError="LayoutRoot_BindingValidationError" >

<TextBox x:Name="QuantityOnHand"   
    Text="{Binding Mode=TwoWay, Path=QuantityOnHand, 
        NotifyOnValidationError=true,  ValidatesOnExceptions=true }"
    VerticalAlignment="Bottom"
    HorizontalAlignment="Left"
    Height="30" Width="90"red
    Grid.Row="4" Grid.Column="1" />


// book.cs

public int QuantityOnHand
{
   get { return quantityOnHand; }
   set
   {
      if ( value < 0 )
      {
         throw new Exception( "Quantity on hand cannot be negative!" );
      }
      quantityOnHand = value;
      NotifyPropertyChanged( "QuantityOnHand" );
   }       // end set
}
Yuval Peled
I was expecting to see which control caused the exception in the ValidationErrorEventArgs (http://msdn.microsoft.com/en-us/library/system.windows.controls.validationerroreventargs.aspx). So I moved the BindingValidationError event off the Grid, and onto the textboxes which can cause an exception. This way I can check which control caused in the exception in the sender parameter.
russau
A: 

You might want to look at PostSharp, it makes attributing your client-side data model very simple.

Craig Nicholson
If you provide an example of how to use PostSharp to aid in validation, then I'll remove the downvote.
Andrew Garrison
+1  A: 

If you are experiencing problems trying to implement this, it's not because your code is broken, it's because the feature is broken in the DataGrid. Check out the article by Jesse Liberty here.

The correct link: http://jesseliberty.com/2008/10/22/it-aint-you-babe%E2%80%A6-a-not-a-bug-bug-in-datagrid/
Lukas Cenovsky
@Lukas: link fixed.
Pourquoi Litytestdata