本文介绍了在AddNew或EditItem事务mvvm期间不允许WPF DataGrid'Refresh'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下网格

    <DataGrid

        x:Name="TablesDataGrid"
        Grid.Column="0"
        Grid.Row="1"
        ItemsSource="{Binding FilteredModels.View}"
        AlternationCount="2"
        AutoGenerateColumns="False"
        CanUserSortColumns="True"
        CanUserReorderColumns="False"
  CanUserDeleteRows="False"
  CanUserAddRows="False"
  SelectionMode="Extended"
        IsReadOnly="False"
  SelectionUnit="FullRow"
        RowHeight="25"
  HorizontalAlignment="Stretch"
  ColumnWidth="Auto">
            <DataGrid.Columns >
                <DataGridCheckBoxColumn Width="*" Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  IsReadOnly="False">
                    <DataGridCheckBoxColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.CheckAll}"/>
                        </DataTemplate>
                    </DataGridCheckBoxColumn.HeaderTemplate>
                </DataGridCheckBoxColumn>
                <DataGridTextColumn Header="Source Table" Binding="{Binding SourceTableFullName}" Width="4*"></DataGridTextColumn>
                <DataGridTextColumn Header="EDW Schema"  Binding="{Binding SchemaName}" Width="2*"></DataGridTextColumn>
                <DataGridTextColumn Header="EDW Table" Binding="{Binding TableName}" Width="4*"></DataGridTextColumn>
                <DataGridTextColumn Header="Status" Binding="{Binding Status}" Width="*"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>

然后我有一个seachCommand,可以在viewmodel中的collectionViewSource FilteredModels上执行搜索,然后调用

and then i have a seachCommand with performs the search on the collectionViewSource FilteredModels in the viewmodel and then calls

this.FilteredModels.View.Refresh();

当用户选中几个复选框并将网格发送到editmode然后执行搜索时,我们会收到以下错误

when a user checks a few of the checkboxes and sends the grid into editmode and then performs a search we get the following error

WPF DataGrid 'Refresh' is not allowed during an AddNew or EditItem transaction

是否有一种方法可以在选中复选框时甚至在单击搜索按钮或其他修复方法时将网格强制退出编辑模式?

is there a way to force the grid out of edit mode when a check box is checked or maybe even when the seach button is clicked or some other fix for this?

谢谢!

推荐答案

我知道回答来不及了……但是对于正在寻找答案的人

I know its too late to answer...but for someone who is looking for answer

按这样的顺序两次使用cancelEdit或commitEdit方法

use cancelEdit or commitEdit method two times in a sequence like this

//用于提交

this.datagrid_layers.CommitEdit();
this.datagrid_layers.CommitEdit();

//取消

this.datagrid_layers.CancelEdit();
this.datagrid_layers.CancelEdit();

这篇关于在AddNew或EditItem事务mvvm期间不允许WPF DataGrid'Refresh'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:32