tags:

views:

886

answers:

0

In Silverlight 3 I am working with a MVVM and also the validation principle that the setters cause an exception if a validation error occurs. I using the Binding Syntax on the fields using TwoWay i.e.:

<TextBox x:Name="TextBoxClientName" Text="{Binding Name,Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=true}"  Grid.Column="1" Grid.Row="0" Margin="5 5 5 5" />

I validate this property in the ViewModel using the Annotations:

[Required(ErrorMessage = "Name is required")]
public string Name
{
    get
    {
        return _client.Name;
    }
    set
    {
        Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "Name", DisplayName="Client Name" });
        _client.Name = value;
    }
}

I have the Validation summary and all work well BUT lol, the functionality I am looking for is the following:

You have the Data Form and I want the validation summary to appear over the top ONLY when I click save, and further more I want to implement a close button on that ValidationSummary so the user can continue on with entering and correcting.

I am not sure how to control the visibility or toggle of with the validationsummary, I have tried the Visibility. The following is code that I tried, WHICH does collect the errors on submit, but I cannot apply them to the validationsummary:

    public void Save()
    {
        List<ValidationError> errors = new List<ValidationError>();

        foreach (UIElement ui in LayoutRoot.Children)
        {
            FrameworkElement fe = ui as FrameworkElement;

            if (fe != null)
            {
                foreach (ValidationError ve in Validation.GetErrors(fe))
                {
                    errors.Add(ve);
                }
            }
        }


        if (errors.Count > 0)
        {

            Validation1.DataContext = errors;
            Validation1.Filter = ValidationSummaryFilters.All;
        }
        else
        {
            if (Saved != null)
                Saved(this, EventArgs.Empty);
        }

    }

Cheers,

Andrew