'ICollectionView DataGrid is staying updated even after cancelling edit window

I have a problem and maybe someone had this situation in his C#/WPF/MVVM app.

I have the DataGrid populated by data from database (using Prism.MVVM), also I am using ICollectionView to can filter this DataGrid in realtime.

For editing I have new dialog window with textboxes etc. binded to SelectedItem. Everything is working fine except one thing. When I edit some values in (lets called it) "Edit Mode", data is updated real time in DataGrid and this is okay (update to database is after I press "Save" button), but even if I press Cancel button, values stayed updated in DataGrid.

Database is not getting updated as it supposed, but datagrid does. Using .Refresh() method of ICollectionView do nothing, only way to get it back to normal is restarting an app.

XAML for DataGrid in View (simplified for readability):

                    <DataGrid ColumnWidth="Auto" Grid.Row="1" ItemsSource="{Binding ProductsCollectionView}" SelectedItem="{Binding SelectedItem}" AutoGenerateColumns="False" IsReadOnly="True"
                                ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible"  ScrollViewer.HorizontalScrollBarVisibility="Auto">
                    <DataGrid.InputBindings>
                        <MouseBinding Gesture="LeftDoubleClick" Command="{Binding EditCommand}"/>
                    </DataGrid.InputBindings>

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Sygnatura" Binding="{Binding Signature}"/>
                        <DataGridTextColumn Header="Nazwa" Binding="{Binding Name}" />
                        <DataGridTextColumn Header="Stan" Binding="{Binding InStock}" CellStyle="{StaticResource CenterValues}"/>
                        <DataGridTextColumn Header="Cena zakupu" Binding="{Binding PurchasePrice, StringFormat=N2}" CellStyle="{StaticResource CenterValues}"/>
                        <DataGridTextColumn Header="Cena SRP" Binding="{Binding SellPrice, StringFormat=N2}" CellStyle="{StaticResource CenterValues}"/>
                        <DataGridTextColumn Header="Cena FRN" Binding="{Binding FranPrice, StringFormat=N2}" CellStyle="{StaticResource CenterValues}"/>
                        <DataGridTextColumn Header="Rodzaj produktu" Binding="{Binding ProductType.Name}" CellStyle="{StaticResource CenterValues}"/>
                        <DataGridTextColumn Header="Manager" Binding="{Binding Seller.Symbol}" CellStyle="{StaticResource CenterValues}"/>
                        <DataGridTextColumn Header="Opakowanie" Binding="{Binding Package.Symbol}" CellStyle="{StaticResource CenterValues}"/>
                    </DataGrid.Columns>
                </DataGrid>

ViewModel (For window with DataGrid):

public class ProductListViewModel : BindableBase
{
    public ICollectionView ProductsCollectionView { get; }
    private ProductService _service;

    private ObservableCollection<Product> _data;
    public ObservableCollection<Product> Data
    {
        get => _data;
        set => SetProperty(ref _data, value);
    }

    private Product _selectedItem;
    public Product SelectedItem
    {
        get => _selectedItem;
        set => SetProperty(ref _selectedItem, value);
    }

    private string _filterBar = string.Empty;
    public string FilterBar
    {
        get { return _filterBar; }
        set
        {
            SetProperty(ref _filterBar, value);
            ProductsCollectionView.Refresh();
        }
    }

    public ProductListViewModel()
    {
        _service = new ProductService();
        Load();

        ProductsCollectionView = CollectionViewSource.GetDefaultView(Data);
        ProductsCollectionView.Filter = FilterProducts;

        LoadCommands(); // <- This contains: EditCommand = new DelegateCommand(Edit);
    }

    public void Load()
    {
        Data = new ObservableCollection<Product>(_service.GetAllProducts());
    }

    private void Edit()
    {
        var dialog = new ProductView(SelectedItem);

        dialog.ShowDialog();

        if (dialog.DialogResult == true)
        {
            _service.UpdateProductData(SelectedItem);
            _service.SaveChanges();
            ProductsCollectionView.Refresh();
        }
    }

ProductView (Example binding)

        <StackPanel Margin="0,0,13,0" Grid.Column="1">
            <TextBlock Text="Sygnatura" Style="{StaticResource StackPanelTextBox}"/>
            <TextBox Text="{Binding Data.Signature}"/>
        </StackPanel>

ProductViewModel

public class ProductViewModel : BindableBase
{
    private Product _data;
    public Product Data
    {
        get => _data;
        set => SetProperty(ref _data, value);

    }

    public ICommand Save { get; set; }
    public ICommand Cancel { get; set; }

    public ProductViewModel(Product data)
    {

        EditMode();
        LoadSellers();
        LoadProductTypes();
        LoadPackages();

        Data = data;

        Save = new DelegateCommand(() => SaveAction?.Invoke()); // Sets dialog.DialogResoult == true
        Cancel = new DelegateCommand(() => CancelAction?.Invoke());  // Sets dialog.DialogResoult == false

    }
}

I will be glad for help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source