title: Edit ListBox on MainWindow from a class
body: <p>I have a WPF application, i created a new class named <code>Agent</code>. On my WPF application window i have <code>ListBox</code>. I call <code>Agent</code> from the MainWIndow.xaml.cs .</p>
  
  <p>Agent class runs a <code>FileSystemWatcher</code> and now when <code>OnChanged</code> event is raised i want to add the message to the <code>ListBox</code> that the event was raised. </p>
  
  <p>MainWindow.xaml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class="MachineLogAgentGUI.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="411" Width="515"&gt;
  &lt;Grid Background="#0A000000"&gt;
      &lt;Button Content="Run Agent" Height="26" HorizontalAlignment="Left" Margin="397,12,0,0" Name="runAgent" VerticalAlignment="Top" Width="86" Click="runAgent_Click" /&gt;
      &lt;ListBox Margin="12,66,12,12" Name="messageBox" /&gt;
      &lt;TextBox Height="26" HorizontalAlignment="Left" Margin="12,12,0,0" Name="observedDirectory" VerticalAlignment="Top" Width="261" /&gt;
      &lt;Button Content="Browse" Height="26" HorizontalAlignment="Left" Margin="271,12,0,0" Name="browse" VerticalAlignment="Top" Width="75" Click="browse_Click" /&gt;
      &lt;CheckBox Content="Include Subfolders" Height="16" HorizontalAlignment="Left" Margin="12,44,0,0" Name="includeSubfolders" VerticalAlignment="Top" /&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>In MainWindow.xaml.cs i have:</p>
  
  <pre><code>    Agent agent = null;
  
      private void runAgent_Click(object sender, RoutedEventArgs e)
      {
          if (agent == null || !agent._running)
          {
              agent = new Agent(@"W:\MindWare.SVN\CardMax2\trunk\ProcessEngine\TSPHelper.Producer\bin\Debug\GeneratedLogs");
              runAgent.Content = "Stop Agent";
          }
          else if (agent._running)
          {
              agent.StopAgent();
              runAgent.Content = "Run Agent";
          }
      }
  </code></pre>
  
  <p>And Agent class:</p>
  
  <pre><code>public class Agent
  {
      private string Path { get; set; }
      public bool _running { get; set; }
  
      FileSystemWatcher watcher = new FileSystemWatcher();
  
      [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
      public Agent(string path)
      {
          Path = path;
          watcher.Path = path;
          watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
             | NotifyFilters.FileName | NotifyFilters.DirectoryName;
          watcher.Filter = "*.txt";
          watcher.IncludeSubdirectories = true;
  
          watcher.Changed += new FileSystemEventHandler(OnChanged);
  
          watcher.EnableRaisingEvents = true;
          _running = true;
      }
  
      // Stop Agent
      public void StopAgent()
      {
          watcher.EnableRaisingEvents = false;
          _running = false;
      }
  
      // Define the event handlers.
      private void OnChanged(object source, FileSystemEventArgs e)
      {        
          try
          {
              watcher.EnableRaisingEvents = false;
              _running = false;
  
              // DO SOMETHING HERE
              // Add item to ListBox on MainWindow somehow
          }
          finally
          {
              watcher.EnableRaisingEvents = true;
              _running = true;
          }
      }
  </code></pre>
  
  <p>How i could do that?</p>