0
votes

My problem is the following:

I have a ChildWindow that gets an Entity that is editable.

I use DataAnnotation attributes to set the validation rules.

My Entity has a Name property and Required attribute is applied on it.

If I modify an existing Entity and I clear the textbox that is binding to the Name property I get a validation error that the name field is required.

But when I add a new entity and the Name property is null the validation doesn't work.

I set the Name property to String.Empty in the constructor of the class and I display the Entity in the childwindow the validation error is showing in the textbox.

What is the best solution?

Can I set the button that is showing the childwindow to doesn't fire the validation?

In Asp.NET the button control has a IsCausesValidation property that is false the button doesn't fire validation.

Thanks advance l,

1

1 Answers

0
votes

What are you trying to achieve? A validation exception won't get thrown until you actually set the property via a setter on the underlying bound object. Do you want all the validation errors to be displayed when a user first enters a data entry form? Are you using a data form? If so you should be able to validate the entire form at the time of saving the data e.g.

    private void OKButton_Click(object sender, RoutedEventArgs e)
    {
        bool valid = MyDataForm.ValidateItem();
        if (valid)
        {
            MyDataForm.CommitEdit(true);
            this.DialogResult = true;
        }
    }