Skip to main content
 首页 » 编程设计

c#之在 AddNew 或 EditItem 事务期间不允许延迟刷新

2024年02月06日19开发

我有一个包含 2 个数据网格的窗口。如果我从第一个 DataGrid 中的一个特定列单击到另一个 DataGrid 的任何列,则会出现错误

DeferRefresh is not allowed during an AddNew or EditItem transaction

这里出了什么问题?

第一个DataGrid是

<DataGrid x:Name="FirstDataGrid" 
          ItemsSource="{Binding Parts, Mode=TwoWay}" 
          SelectedItem="{Binding SelectedPart, Mode=TwoWay}" 
          CellEditEnding="DataGrid_OnCellEditEnding" > 
     <i:Interaction.Behaviors> 
          <views:ScrollIntoViewBehavior /> 
     </i:Interaction.Behaviors> 
          <DataGrid.Resources> 
               <Style TargetType="{x:Type DataGridCell}"> 
                   <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter> 
               </Style> 
          </DataGrid.Resources> 
      <DataGrid.Columns> 
          <DataGridTemplateColumn> 
                    <DataGridTemplateColumn.CellTemplate> 
                        <DataTemplate> 
                            <ItemsControl ItemsSource="{Binding Identifications, Mode=OneWay}"> 
                                <ItemsControl.ItemsPanel> 
                                    <ItemsPanelTemplate> 
                                        <StackPanel Orientation="Horizontal"/> 
                                    </ItemsPanelTemplate> 
                                </ItemsControl.ItemsPanel> 
                                <ItemsControl.ItemTemplate> 
                                    <DataTemplate> 
                                        <TextBlock Margin="5,0,0,0"> 
                                            <Hyperlink NavigateUri="{Binding ArticleNumber, Mode=OneWay}"  
                                                        Command="{Binding ElementName=PartDataGrid, Path=DataContext.OpenIdentificationCommand}" CommandParameter="{Binding}" >                                     
                                                <TextBlock Text="{Binding ArticleNumber, Mode=OneWay}"/> 
                                            </Hyperlink> 
                                        </TextBlock> 
                                    </DataTemplate> 
                                </ItemsControl.ItemTemplate> 
                            </ItemsControl> 
                        </DataTemplate> 
                    </DataGridTemplateColumn.CellTemplate> 
                </DataGridTemplateColumn> 
      </DataGrid.Columns> 
</DataGrid> 

第二个 DataGrid 无关紧要,因为我可以单击任何列来产生错误。

请您参考如下方法:

自己解决了。

导致问题的列是只读的。那么为什么还要允许编辑模式呢?那显然是错误的。我通过添加

解决了这个问题
IsReadOnly="True" 

到 WPF 列定义。