Silverlight 3, Validation, MVVM, WCF and EF

All the current articles and tutorials on Silverlight 3 Validation show really simplistic examples, business objects in the UI space, no services etc etc

My Problem

When the business objects are being created and managed via the entity framework, there does not appear to be any simple ways of adding the validation properties to the object fields. This means that by the time the object gets to the UI, we have no control.

in addition to this I need to be able to provide UI base validation and messaging based on user inputs. This has to happen on the field edit and also on “Save click”. Not as easy as it sounds.

MY Workaround

Anyway, I have a workaround that appears to be doing what I need. This involves a few more tweaks than I feel is appropriate, so I’d be interested to hear your thoughts on this approach. Anyway, our form is as follows:

Previously we had a business object in the ViewModel (FocusedDepartment) and were binding the values of the textboxes to the fields of this object, for example:

<TextBox x:Name="Txt_Number" Text="{Binding Mode=TwoWay, Path=FocusedDepartment.DepartmentId}" />

The problem with this was the FocusedDepartment type (Department) was derived from the service layer, which in turn had come from our EF model. We did not find a solution to attach the validation properties to the EF model objects. The workaround for this was to add specific properties into the ViewModel to support the fields, and attach validation properties to these:

This property still uses the FocusedDepartment property as a backing store. In the xaml we changed our binding to use this new property:

<TextBox x:Name="Txt_Number" Text="{Binding Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True, Path=DepartmentId}" />

The FocusedDepartment property also had to notify property changes for the new fields:

//the currently selected item, bound to the datacontext of the view
private Department _focusedDepartment = new Department();
public Department FocusedDepartment
get { return _focusedDepartment; }
_focusedDepartment = value;


From this point we where able to see the validation on the text boxes appear correctly but only when the values in the textboxes had been changed.

When tabbing through the fields, the bound fields where not getting touched, so the validation was not occurring. To workaround this issue I have added some code to the Save button click event (sorry MVVM purists!!) to update the source of the binding expressions, then checked the error count against the ViewModel before submitting the save action:

Firstly, I have a handler to manage the error count in the VM

Then handled the save button click in code behind

This ensures the fields are valid (and the validation messages are displayed) before committing the Save action to the service.

This approach (particularly the checking and setting of values directly in the VM) feels pretty dirty at the moment L, but it does seem to work for UI validation.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

A Website.

Up ↑

%d bloggers like this: