检查是否有任何 WPF DataGrid 单元格有错误 [英] Checking If Any WPF DataGrid Cell Has Error

查看:21
本文介绍了检查是否有任何 WPF DataGrid 单元格有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单元格上设置了验证,它按预期工作(在文本框周围放置红色突出显示并添加带有错误的工具提示).但是,如果我尝试访问 Validation.GetHasError(TheGrid) 其中 TheGrid 是我的 DataGrid,则它始终为 false.有谁知道如何检查 DataGrid 中的任何单元格是否有错误?

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
}

这篇关于检查是否有任何 WPF DataGrid 单元格有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆