title: looping through a folder of images in c# / WPF
body: <p>So i'm trying to loop through a folder and change the image source each 2 seconds. </p>
  
  <p>I think my code is right, but I seem to be missing something since my image won't update, but I don't get an error.</p>
  
  <p>The code populates my array of files so it finds the pictures, I'm just doing something wrong to set the image source.</p>
  
  <p>XAML code</p>
  
  <pre><code>&lt;Grid&gt;
         &lt;Image x:Name="Picture" Source="{Binding ImageSource}"  Width="980" Height="760" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="350,50,0,0"&gt;&lt;/Image&gt;
   &lt;Grid&gt;
  </code></pre>
  
  <p>C# code</p>
  
  <pre><code> private string[] files;
      private System.Timers.Timer timer;
  
      private int counter;
      private int Imagecounter;
  
      Uri _MainImageSource = null; 
  
      public Uri MainImageSource {
          get 
          {
              return _MainImageSource; 
          } 
          set 
          {
              _MainImageSource = value;
          } 
      }
  
      public IntroScreen()
      {
          InitializeComponent();
          this.Loaded += new RoutedEventHandler(this.MainWindow_Loaded);
      }
  
      private void MainWindow_Loaded(object sender, RoutedEventArgs e)
      {
  
          setupPics();
      }
  
      private void setupPics() 
      {
          timer = new System.Timers.Timer();
          timer.Elapsed += new ElapsedEventHandler(timer_Tick);
          timer.Interval = (2000);
          timer.Start();
  
          files = Directory.GetFiles("../../Resources/Taken/", "*.jpg", SearchOption.TopDirectoryOnly);
          Imagecounter = files.Length;
          MessageBox.Show(Imagecounter.ToString());
          counter = 0;
      }
  
      private void timer_Tick(object sender, EventArgs e)
      {
          counter++;
          _MainImageSource = new Uri(files[counter - 1], UriKind.Relative);
          if (counter == Imagecounter)
              {
                  counter = 0;
              }
      }
  </code></pre>
  
  <p>Anyone know what I'm doing wrong ?</p>
  
  <p>Updated code</p>
  
  <p>XAML</p>
  
  <pre><code> &lt;Image x:Name="Picture" Source="{Binding MainImageSource}"  Width="980" Height="760" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="350,50,0,0"&gt;&lt;/Image&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>public partial class IntroScreen : UserControl, INotifyPropertyChanged
  {
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected virtual void OnPropertyChanged(string propertyName)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
              handler(this, new PropertyChangedEventArgs(propertyName));
      }
  
      private string[] files;
      private System.Timers.Timer timer;
  
      private int counter;
      private int Imagecounter;
  
      Uri _MainImageSource = null;
  
      public Uri MainImageSource
      {
          get 
          {
              return _MainImageSource; 
          } 
          set 
          {
              _MainImageSource = value;
              OnPropertyChanged("MainImageSource");
          } 
      }
  
      public IntroScreen()
      {
          InitializeComponent();
          this.Loaded += new RoutedEventHandler(this.MainWindow_Loaded);
      }
  
      private void MainWindow_Loaded(object sender, RoutedEventArgs e)
      {
  
          setupPics();
      }
  
      private void setupPics() 
      {
          files = Directory.GetFiles("../../Resources/Taken/", "*.jpg", SearchOption.TopDirectoryOnly);
          Imagecounter = files.Length;
  
  
          counter = 0;
          timer = new System.Timers.Timer();
          timer.Elapsed += new ElapsedEventHandler(timer_Tick);
          timer.Interval = (2000);
          timer.Enabled = true;
          timer.Start();
  
  
      }
  
      private void timer_Tick(object sender, EventArgs e)
      {
          counter++;
          MainImageSource = new Uri(files[counter - 1], UriKind.Relative);
          if (counter == Imagecounter)
              {
                  counter = 0;
              }
      }
  </code></pre>
  
  <p>I'm not getting any error's but the image still isen't switching. I'm wondering if my paths are even working. Is there any way to test this ?</p>