简体   繁体   中英

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM