0

I want to freeze/unfreeze my data in a listview which is receiving polling data every few seconds. I have a button that will freeze/unfreeze the panel, but I want a way to stop the panel from updating visibly in the UI, but continue to have the collection updated in the code under the hood as normal. I tried to change the setter for my bound object, however, it doesn't get hit when I am doing the following:

      Output.Insert(0, newMessage);

      if (Output.Count > this.maxOutputItemCount)
      {
        Output.RemoveAt(this.maxOutputItemCount);
      }

Where Output is the observable collection.

This is the setter that doesn't get hit each time the insert happens, which means the frozen check is ineffective:

    private ObservableCollection<PDMessageModel> output;
    public ObservableCollection<PDMessageModel> Output
    {
      get => this.output;
      set
      {
        if (value == this.output)
        {
          return;
        }

        this.output = value;

        if (!isFrozen)
        {
          RaisePropertyChanged();
        }
      }
    }

I created a function that listens to the CollectionChanged function of ObservableCollection but not sure how I can use this to my advantage...

I have also tried changing to List:

    public List<PDMessageModel> Output { get; private set; }

And changing the code like so:

      if (isFrozen)
      {
        return;
      }

      Output.Insert(0, newMessage);

      if (Output.Count > this.maxOutputItemCount)
      {
        Output.RemoveAt(this.maxOutputItemCount);
      }
      RaisePropertyChanged(nameof(Output));

But the UI is not updating when RaisePropertyChanged is called.

Changing the code to this using a list works:

      if (isFrozen)
      {
        return;
      }

      List<PolledDataMessageModel> outputCopy = new List<PolledDataMessageModel>(Output);
      outputCopy.Insert(0, newMessage);

      if (outputCopy.Count > this.maxOutputItemCount)
      {
        outputCopy.RemoveAt(this.maxOutputItemCount);
      }
      Output = outputCopy;

Though I don't know if that's preferred over using ObservableCollection and having suppression of notifications.

Has anyone got any ideas or a better way of doing this?

Thanks!

sushi7777
  • 179
  • 3
  • 16
  • Well, usually re-initializing an observable collection is not a good idea and you can have a pausable observable collection by developing your own observable collection. In this [so answer](https://stackoverflow.com/a/13303245/12354911) there is an example of it. You should just decide when to pause it. (In the example it is get paused for bulk operations) – Eldar Sep 19 '22 at 10:49
  • @Eldar so if I used that Smart class, and I wanted to stop updating the UI (not the physical collection itself) based on a boolean, how would one do that? Not sure I'm seeing how we can use this to do that. – sushi7777 Sep 19 '22 at 10:53
  • Since you dont want to use the binding that observableColletion provided, why dont you just use List and raise the property changed manually? After any Insert/remove you have to manually call the PropertyChanged based on your isFrozen property. – Hytac Sep 19 '22 at 11:04
  • @Hytac tried that (see above edits in the question) but UI does not seem to update now when RaisePropertyChanged is called – sushi7777 Sep 19 '22 at 11:09
  • Try this: "{Binding Output, UpdateSourceTrigger=PropertyChanged}" – Hytac Sep 19 '22 at 11:19
  • Sadly makes no difference. `Insert` doesn't seem to cause the setter to be triggered. – sushi7777 Sep 19 '22 at 11:23
  • @MuhammadSulaiman potentially. I've got Hytac's solution to work by creating a copy (see edits in question) but I don't know if this is better or to go with the suppression solution. – sushi7777 Sep 19 '22 at 11:28
  • 1
    @MuhammadSulaiman, seems like a nice solution though. Just setting to Suppress_Notification boolean to true and then false when freezing and unfreezing works quite nicely. I might use this solution as it feels architecturally cleaner - thanks! :) – sushi7777 Sep 19 '22 at 11:33

0 Answers0