WPF: Detect row validation errors in C# code

后端 未结 3 724
借酒劲吻你
借酒劲吻你 2021-01-06 07:29

I want to achieve a pretty simple task, but none of the solutions here on SO or otherwise found have helped me so far:

I have a WPF DataGrid, which is b

3条回答
  •  无人及你
    2021-01-06 08:25

    OK, I've worked it out. The following does what I want:

    public static DataGridRow GetRow(DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
    

    In the code for my "OK" button I do:

    for (int i = 0; i < dgvData.Items.Count; i++)
    {
        DataGridRow row = GetRow(dgvData, i);
        if (row != null && Validation.GetHasError(row))
        {
            hasDataGridErrors = true;
            break;
        }
    }
    

提交回复
热议问题