KrisTrip
KrisTrip

Reputation: 5043

Checking If Any WPF DataGrid Cell Has Error

I have validation set up on the cells and it works as expected (placing a red highlight around the textbox and adding a tooltip with the error). However, If I try to access Validation.GetHasError(TheGrid) where TheGrid is my DataGrid, it is always false. Does anyone know how to check if ANY cells in the DataGrid have errors?

I want to do this so I can disable saving if there are errors.

Upvotes: 7

Views: 8433

Answers (1)

brunnerh
brunnerh

Reputation: 184296

You might run into problems with virtualization with this but you probably do have to look at the containers:

var errors = (from c in
                  (from object i in _myGrid.ItemsSource
                   select _myGrid.ItemContainerGenerator.ContainerFromItem(i))
              where c != null
              select Validation.GetHasError(c))
             .FirstOrDefault(x => x);
if (errors)
{
    //There be errors
}

Upvotes: 8

Related Questions