WPF MVVM - Stop value from updating on the UI

Viewed 39

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!

0 Answers
Related