views:

126

answers:

1

Hi All,

I have Silverlight 3 application with two datepickers for Start Date and End Date. They are data bound to a business object which implements validation logic such that the StartDate must be before the EndDate and the EndDate must be after the StartDate.

So far, so good - both controls display the appropriate validation error when a validation exception is thrown in the respective setter.

My problem is if the user changes the 'other' control such that the 'invalid' date in the first control is now valid, the first control's state does not change (because its setter has not been called).

For example, if I set StartDate to 15 Dec 2009 and EndDate to 10 Dec 2009, the EndDate control correctly goes into the invalid state. If the user changes the StartDate to 9 Dec 2009, the EndDate control is still marked as invalid because the UI has not called the EndDate setter.

Is there a 'clean' MVVM-style method of forcing the UI to revalidate?

A: 

use the ValidationScope class from here Basically it allows you to group a few controls and validate that group when a certain command is fired, works nicely for standard stuff.

Xaml for a few textboxes

<StackPanel local:ValidationScope.ValidationScope="{Binding PersonValidationScope}">
        <TextBox
            Text="{Binding Person.Name, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
            local:ValidationScope.ValidateBoundProperty="Text" />
        <TextBox
            Text="{Binding Person.Age, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
            local:ValidationScope.ValidateBoundProperty="Text" />
        <ComboBox
            ItemsSource="{Binding Salutations}"     
         SelectedItem="{Binding Person.Salutation, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"
    local:ValidationScope.ValidateBoundProperty="SelectedItem" />
    <Button Content="Save" Click="SaveButtonClick" />
</StackPanel>

ViewModel looks like this

public void Save()
{
    // This causes all registered bindings to be updated
    PersonValidationScope.ValidateScope();
    if (PersonValidationScope.IsValid())
    {
        // Save changes!
    }
}

Follow the link for the ValidationScope class.

Neil
Thanks Neil, that does the trick nicely.
David Austin