0

So i have this method to read from a CSV File and it works.

//Alte Bestand
    private void PageLoad_Alt(object sender, RoutedEventArgs e)
    {

        try
        {
            //Index von selectedItem
            string filepath = directory + "\\" + "ParkingTool_BESTAND.csv";


            //CSVHelper Config

            var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ";" };
            using (var csv = new CsvReader(new StreamReader(filepath), config))
            {
                csv.Read();
                csv.ReadHeader();

                while (csv.Read())
                {
                    string barcodeField = csv.GetField<string>("Barcode");
                    string boxField = csv.GetField<string>("BoxNr");
                    string datumField = csv.GetField<string>("Datum");
                    alte_parking_collection.Add(new ParkingClass() { parking_barcode = barcodeField, parking_box = boxField, parking_datum = datumField }); ;
                }
            }
            code_box.Focus();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Bitte Datei überprüfen! " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
            code_box.Focus();
        }
    }

Then i call it with Loaded += PageLoad_Alt; when the page starts.

But i want something that calls the method again when a another collection is edited. Any ideas?

parking_collection.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(PageLoad_Alt());

This is not working.

M.Nasiri
  • 37
  • 6

2 Answers2

0

The issue is that Loaded is a routed event requiring RoutedEventArgs, while the CollectionChange event requires CollectionChangedEventArgs (which does not derive from RoutedEventArgs). So you can not use the same parameters. You don't appear to be using the event arguments anyway, so pull your code into a private method and call it from two event handlers.

private void PrintCSV()
{
     try
     {
         //Index von selectedItem
         string filepath = directory + "\\" + "ParkingTool_BESTAND.csv";


         //CSVHelper Config
         var config = new CsvConfiguration(CultureInfo.CurrentCulture) { Delimiter = ";" };
         using (var csv = new CsvReader(new StreamReader(filepath), config))
         {
             csv.Read();
             csv.ReadHeader();

             while (csv.Read())
             {
                 string barcodeField = csv.GetField<string>("Barcode");
                 string boxField = csv.GetField<string>("BoxNr");
                 string datumField = csv.GetField<string>("Datum");
                 alte_parking_collection.Add(new ParkingClass() { parking_barcode = barcodeField, parking_box = boxField, parking_datum = datumField });
             }
      }
         code_box.Focus();
     }
     catch (Exception ex)
     {
         MessageBox.Show("Bitte Datei überprüfen! " + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Warning);
         code_box.Focus();
     }
}

private void PageLoad_Alt(object sender, RoutedEventArgs e) => PrintCSV();
private void OnCollectionChanged(object sender, CollectionChangedEventArgs e) => PrintCSV();

Then subscribe to the appropriate events

Loaded += PageLoad_Alt;
parking_collection.CollectionChanged += OnCollectionChanged;
Tarazed
  • 1,707
  • 1
  • 7
  • 22
0

I am not sure what syntax you are using to attempt to call the method when the collection changes, but this will surely call your method when parking_collection changes:

parking_collection.CollectionChanged += (s, e) => PageLoad_Alt(null, null);

Ensure that you register the above event handler on the current Collection that parking_collection references. For example, if at any point in your code you do something like this:

parking_collection = new ObservableCollection<Parking>();

you need to register the event handler again. You also might need to re-register when a Binding related to parking_collection changes if the collection is part of a binding. (Implementation specific but a common mistake)

purquhart
  • 66
  • 2
  • This won't work. You can't ignore the parameters on PageLoad_Alt and the CollectionChanged event doesn't have the required parameters. At best you could send an empty event args. ```parking_collection.CollectionChanged += (s, e) => PageLoad_Alt(s, new RoutedEventArgs());``` This works but it is creating an unnecessary new object. – Tarazed Nov 22 '22 at 19:42
  • ``EventArgs`` has ``EventArgs.Empty`` but sadly RoutedEventArgs doesn't have a similar static property. Furthermore, because RoutedEventArgs is derived from EventArgs, it still has that property but what it returns is not a RoutedEventArgs. – Tarazed Nov 22 '22 at 19:48
  • You are right, but he does not use the parameters in PageLoad_Alt and does not need to create a new object either. Passing (null, null) will work as well. Edited answer. – purquhart Nov 22 '22 at 19:50
  • Yes that will work as long as you don't have nullable reference types enabled (or aren't bothered by the compiler warnings). sender is nullable but the RoutedEventArgs are not and can't change it to nullable without doing the same kind of event registration for Loaded. This seems a bit convoluted compared to the solution I posted though. – Tarazed Nov 22 '22 at 20:01