Basssprosse
Basssprosse

Reputation: 466

EmptyCell: DeferRefresh is not allowed during an AddNew or EditItem transaction

I have a (.NET Framework 4.6.1) wpf-window with a datagrid bound to a collection on my mainwindow. This collection contains a property of type decimal.

enter image description here

When I clear the cell it gets a red border.

enter image description here

Here is the window-constructor

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

Now I close the window (cell is empty). I reopen the window and debug my variable x. It says the sum is still 200. On next command InitialiseComponent(); it crashes with the error:

DeferRefresh is not allowed during an AddNew or EditItem transaction

I tried FallBackValue and TargetNullValue but both don't catch the empty cell and don't catch this weird exception. I tried CommitEdit() and CancelEdit() but this also doesn't help:

        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();
    }

Here is the xml of my datagrid.

 <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>

Upvotes: 0

Views: 155

Answers (1)

mm8
mm8

Reputation: 169210

A decimal property cannot be set to something like null or an empty string.

You can use a converter to tell WPF to set it back to 0.0 (default(decimal)) when you clear the cell:

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>

Or change the type of BETRAG to Nullable<decimal> (decimal?).

Upvotes: 1

Related Questions