【问题标题】:EmptyCell: DeferRefresh is not allowed during an AddNew or EditItem transactionEmptyCell:在 AddNew 或 EditItem 事务期间不允许 DeferRefresh
【发布时间】:2021-07-15 16:15:27
【问题描述】:

我有一个 (.NET Framework 4.6.1) wpf 窗口,其中一个数据网格绑定到我的主窗口上的一个集合。此集合包含十进制类型的属性。

当我清除单元格时,它会出现红色边框。

这里是窗口构造器

public WdwSapAccounting()
{
    DataContext = System.Windows.Application.Current.MainWindow.DataContext;
    var x = MainWindow.Reference.CurrentAction.ACCOUNTING_COLLECTION;
    InitialiseComponent();
}

现在我关闭窗口(单元格为空)。我重新打开窗口并调试我的variable x。它说总和仍然是 200。在下一个命令 InitialiseComponent(); 它崩溃并出现错误:

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

我尝试了 FallBackValue 和 TargetNullValue,但两者都没有捕捉到空单元格,也没有捕捉到这个奇怪的异常。我尝试了 CommitEdit() 和 CancelEdit() 但这也无济于事:

        private void MetroWindow_Loaded(object sender, RoutedEventArgs e)
    {
        dataGridAccounting.CommitEdit();
        dataGridAccounting.CancelEdit();
    }

    private void MetroWindow_Unloaded(object sender, RoutedEventArgs e)
    {
        dataGridAccounting.CommitEdit();
        dataGridAccounting.CancelEdit();
    }

    private void dataGridAccounting_Loaded(object sender, RoutedEventArgs e)
    {
        dataGridAccounting.CommitEdit();
        dataGridAccounting.CancelEdit();
    }

    private void dataGridAccounting_UnloadingRow(object sender, DataGridRowEventArgs e)
    {
        dataGridAccounting.CommitEdit();
        dataGridAccounting.CancelEdit();
    }

这是我的数据网格的 xml。

 <DataGrid MinHeight="150" Loaded="dataGridAccounting_Loaded" UnloadingRow="dataGridAccounting_UnloadingRow" UnloadingRowDetails="dataGridAccounting_UnloadingRowDetails"
                LoadingRow="DataGridAccounting_LoadingRow" RowStyle="{StaticResource DataGridRowStyle}"
                x:Name="dataGridAccounting" CellEditEnding="dataGridAccounting_CellEditEnding"
                CurrentCellChanged="DataGridAccounting_CurrentCellChanged" Unloaded="dataGridAccounting_Unloaded"
                ItemsSource="{Binding CurrentAction.ACCOUNTING_COLLECTION, UpdateSourceTrigger=PropertyChanged}"
                AutoGenerateColumns="False" >
                </DataGrid.ContextMenu>
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding BETRAG, UpdateSourceTrigger=PropertyChanged, StringFormat=N, FallbackValue=0.0, TargetNullValue=0.0}" Header="BETRAG"/>
                </DataGrid.Columns>
            </DataGrid>

【问题讨论】:

    标签: c# .net wpf xaml data-binding


    【解决方案1】:

    decimal 属性不能设置为 null 或空字符串。

    当您清除单元格时,您可以使用转换器告诉 WPF 将其设置回 0.0 (default(decimal)):

    public class DecimalConverter : IValueConverter
    {
        public string StringFormat { get; set; } = "N";
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
            value;
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) =>
            decimal.TryParse(value?.ToString(), NumberStyles.Any, culture, out decimal d) ? d : default;
    }
    

    XAML:

    <DataGrid x:Name="dg" AutoGenerateColumns="False" >
        <DataGrid.Resources>
            <local:DecimalConverter x:Key="DecimalConverter" />
        </DataGrid.Resources>
        <DataGrid.Columns>
            ...
            <DataGridTextColumn
                        Binding="{Binding BETRAG, StringFormat=N, FallbackValue=0.0, TargetNullValue=0.0,
                            Converter={StaticResource DecimalConverter}}"
                        Header="BETRAG"/>
        </DataGrid.Columns>
    </DataGrid>
    

    或者将BETRAG的类型改为Nullable&lt;decimal&gt;decimal?)。

    【讨论】:

    • 我不想将数据类型更改为可为空。带有转换器的解决方案看起来非常棒,非常感谢!我很快就会试试这个!
    • 感谢您的提醒,同时很抱歉没有接受它。非常感谢您的帮助!
    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2013-11-30
    相关资源
    最近更新 更多