title: WPF C# ObservableCollection Not updating GUI
body: <p><strong>Thanks to the advice of tencntraze this question has been solved.</strong></p>
  
  <p>See the bottom of this question and tencntraze's answer for the solution.</p>
  
  <p>I have a rather strange bug in my program that I'm currently wrestling with.</p>
  
  <p>I'm developing an MVVM application, and I have a simple ListBox in my View with its ItemsSource bound to an ObservableCollection of strings. Whenever I add an item to this collection, it /does/ fire an event to notify that the collection has changed, yet this isn't reflected on the GUI during run-time unless I attempt to manually resize the window as the program is running, almost as if a resizing the window forces the GUI to refresh. </p>
  
  <p>The XAML for the relevant control is as follows</p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding Path=LogWindow}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" /&gt;
  </code></pre>
  
  <p>Which as you can see is perfectly normal. In my ViewModel for this view, I have a public property called LogWindow defined as follows</p>
  
  <pre><code>public ObservableCollection&lt;string&gt; LogWindow
      {
          get
          {
              return _logWindow;
          }
  
          set
          {
              _logWindow = value;
              OnPropertyChanged("LogWindow");
          }
      }
  </code></pre>
  
  <p>The private member for this is as follows</p>
  
  <pre><code>private ObservableCollection&lt;string&gt; _logWindow;
  </code></pre>
  
  <p>In the ViewModel constructor I initialize _logWindow and wire up LogWindow CollectionChanged event to a handler which in turn fires a PropertyChanged notification.</p>
  
  <p>My code for this is as follows</p>
  
  <pre><code>public MainWindowViewModel(IDialogService dialogService)
          {
              ... skipped for brevity
              LogWindow = new ObservableCollection&lt;string&gt;();
              LogWindow.CollectionChanged += LogWindow_CollectionChanged;
              ...
          }
  </code></pre>
  
  <p>The CollectionChanged event handler is as follows</p>
  
  <pre><code>void LogWindow_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
          {
              OnPropertyChanged("LogWindow");
          }
  </code></pre>
  
  <p>And finally when my View receives notification that it has received a new message from the Model, I add a new string to the observable collection as follows</p>
  
  <pre><code>LogWindow.Add(_vehicleWeightManager.SerialMessage);
  </code></pre>
  
  <p>When LogWindow.Add happens, my event listener fires and my breakpoint is hit to confirm the code gets that far as illustrated below
  <img src="https://i.stack.imgur.com/srSYJ.png" alt="Break Point is Hit upon Adding a new string to the collection...">
  <img src="https://i.stack.imgur.com/nEFOF.png" alt="Notification Collection Changed Event args"></p>
  
  <p>After this my code calls the OnPropertyChanged function which is inherited from my ViewModelBase, this functions fine for my other GUI elements such as labels and the like, so that almost certainly isn't the issue.</p>
  
  <p>I applied a watch to LogWindow to keep track that the collection was indeed being added to, and it is as illustrated below
  <img src="https://i.stack.imgur.com/eE4Xz.png" alt="LogWindow watch"></p>
  
  <p>So at this stage I'm at a loss. My GUI will only update if I resize the window in some way, otherwise there is no visual update. The data is present in the bound property, and afaik the GUI is alerted when a new item is added to the property... Does anyone have any insight that may help?</p>
  
  <p><strong>Thanks to the advice of tencntraze and kallocain this has been solved.</strong></p>
  
  <p>The problem was caused by another thread attempting to add to the LogWindow collection which was causing erroneous behaviour. Rather than use Dispatcher as suggested, I used a TaskFactory code as follows.</p>
  
  <pre><code>TaskFactory uiFactory;
  ...
  ...
  public MainWindowViewModel(IDialogService dialogService)
          {
              ...
              _logWindow = new ObservableCollection&lt;string&gt;();
              uiFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());
          }
  ...
  ...
  void _vehicleWeightManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
          {
              ...
              if (e.PropertyName == "VehicleWeight")
              {
                  // Marshal the SerialMessage property to the GUI thread.
                  uiFactory.StartNew(() =&gt; LogWindow.Add(_vehicleWeightManager.SerialMessage.Replace("\n", ""))).Wait();
              }
          }
  </code></pre>
  
  <p>Further reading for the above method can be found <a href="https://stackoverflow.com/questions/7473973/is-it-wrong-to-use-the-dispatcher-within-my-viewmodel">here</a></p>