我有一个TabControl,其中包含几个TabItem,每个TabItem都包含一个类似的绑定WPF DataGrid(注意仅列出相关的XAML)
<DataGrid Height="Auto" HorizontalAlignment="Stretch" Margin="6,6,0,0" Name="xxxxGrid" VerticalAlignment="Top" Width="Auto" AutoGenerateColumns="False" ItemsSource="{Binding Path=xxxxDetailsData}" SelectionMode="Single"
SelectionUnit="FullRow"
GridLinesVisibility="Horizontal"
CanUserDeleteRows="True"
CanUserAddRows="True"
ColumnHeaderHeight="50"
SelectionChanged="xxxxGrid_SelectionChanged">
<DataGrid.RowValidationRules>
<validators:xxxxValidationRule ValidationStep="UpdatedValue" />
</DataGrid.RowValidationRules>
我有一个Save命令,用于验证每个TabItem中的所有DataGrid - 在验证中我希望循环通过每个数据网格的每个DataGrid行并检查我的ValidationRule条件标记的任何错误 - 要做我有
foreach (T dataGridDetail in dataGridDetailCollection)
{
DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromItem(dataGridDetail) as DataGridRow;
bool hasError = Validation.GetHasError(row);
...
}
但是当我不已经点击了一个标签(记住每个标签包含一个DataGrid)时,&#34; dataGrid.ItemContainerGenerator.ContainerFromItem(dataGridDetail)&#34; part返回null。 (注意当我已经单击选项卡以显示其DataGrid时,一切正常并且上面的语句按预期返回DataGridRow(非空)
要解决此问题,我已添加
if (row == null)
{
// May be virtualized, bring into view and try again.
grid.UpdateLayout();
grid.SelectedIndex = index; //index is incremented for each iteration
grid.ScrollIntoView(grid.Items[grid.SelectedIndex]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
但这没有区别 - &#34; grid.ItemContainerGenerator.ContainerFromIndex(index)&#34; part返回null
在DataGrid XAML定义中,我也试过
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Standard"
...但这再次没有区别 - 我仍然得到一个空的回复
有什么想法吗?