Question

I'm having trouble concerning validation in DataGrid. I'm using IDataErrorInfo validation in model classes.

The problem is in editable DataGrid with separate CellTemplate and CellEditingTemplate (Note is a not-null property - validation returns Error if null or empty):

<!-- some other validated columns -->
<DataGridTemplateColumn Header="Note">
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>                                    
            <TextBox Name="textBoxNote" Text="{Binding Note, ValidatesOnDataErrors=True}" />                                    
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Note}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

On Save button, I check MyObject.Error validation property and if not null I show a MessageBox. The problem is that when changing the first column (not the Note one) to a valid value and then clicking Save button, the .Error property is null - this is expected (although unwanted) behavior because the binding with ValidatesOnDataError on Note property never happened (the TextBox control never even existed!). But if I set the ValidatesOnDataErrors to true on TextBlock then I get the undesired validation on every object shown in a DataGrid (say from a database) that I'm not concerned about; validation can also take a lot of time in this case...

What would be the proper way to handle this issue? I would like to keep validation in model classes (the object should know about whether it's valid or not). Is there any way to force validation of a row-bound object in codebehind (Save button event)? Or should I somehow initialize .Error on object construction? Any other ideas?

EDIT: how could I put the whole row (all cells) into edit mode (CellEditingTemplate)? Then, all of the controls would be loaded and databound, this also means validated...

Thanks to all, DB

Était-ce utile?

La solution

Ok, I managed to revalidate the IDataErrorInfo object - kind of forced IDataErrorInfo validation. Otherwise I could add new object to the DataGrid, but properties (except of the edited ones) never got validated.

In the superclass of all of my model objects (that extends IDataErrorInfo) I added this method:

public virtual void Revalidate() // never needed to override though
{
    Type type = this.GetType();

    // "touch" all of the properties of the object - this calls the indexer that checks
    // if property is valid and sets the object's Error property 
    foreach (PropertyInfo propertyInfo in type.GetProperties())
    {                
        var indexerProperty = this[propertyInfo.Name];
    }
}

Now when the user adds new object to DataGrid I manually call myNewObject.Revalidate() method to set the Error property which I check before saving the object to the database. Maybe this is not the best possible solution, but it works for me very painlessly.

Thanks and regards, DB

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top