title: WPF: Binding Pictures to a grid locks them
body: <p>I found questions with almost the same problem, but not a good answer:</p>
  
  <p>I download some pictures with a backgroundworker and set them in a listview, something like this:</p>
  
  <pre><code>    void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
      {
          try
          {
              lsvHeros.Dispatcher.BeginInvoke(new Action(() =&gt; lsvHeros.ItemsSource = null));
              _heros = CountersHandler.GetCounters(e.Argument.ToString());
          }
          catch (Exception ex)
          {
              _heros = new List&lt;Hero&gt;();
          }
      }
  </code></pre>
  
  <p>The Grid is defined like this:</p>
  
  <pre><code>            &lt;ListView x:Name="lsvHeros" Height="140"  Width="160"&gt;
                  &lt;ListView.View&gt;
                      &lt;GridView&gt;
                          &lt;GridViewColumn Header="" Width="40"&gt;
                              &lt;GridViewColumn.CellTemplate&gt;
                                  &lt;DataTemplate&gt;
                                      &lt;Image Source="{Binding HeroIcon}" Width="24" Height="24"  /&gt;
                                  &lt;/DataTemplate&gt;
                              &lt;/GridViewColumn.CellTemplate&gt;
                          &lt;/GridViewColumn&gt;
                          &lt;GridViewColumn  Width="110" DisplayMemberBinding="{Binding HeroName}" Header="" /&gt;
                      &lt;/GridView&gt;
                  &lt;/ListView.View&gt;
              &lt;/ListView&gt;
  </code></pre>
  
  <p>Then I set the itemssourc in the done event:</p>
  
  <pre><code>    void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
      {
  
          lsvHeros.ItemsSource = _heros;
          SetWaitMode(false);
          txbHeroName.Select(0, txbHeroName.Text.Length);
          txbHeroName.Focus();
      }
  </code></pre>
  
  <p>The problem: Even if i set the itemssource to null, the pictures I downloaded are still locked.
  Its definitely a problem of the binding, because when I dont bind them, I can delete them whenever I want.</p>
  
  <p>Event if I want to delete the pictures in te folder manually, it tells me they are locked by another process.</p>
  
  <p>So is there a proper solution to unlock the pictures so I can delete them when I rerun the program?</p>
  
  <p>Thanks for your response</p>
  
  <p>Müller Matthias</p>
  
  <p>P.S. didnt think it matter how I download the files. I basically download the whole html text of a page, search for some patterns I know and then download. Here is the code:</p>
  
  <pre><code>    private static Hero HandleCounter(string str)
      {
          const string IMG = @"img src=""";
          Hero result = new Hero();
  
          string picture = str.Substring(str.IndexOf(IMG) + IMG.Length);
          picture = picture.Substring(0, picture.IndexOf("png", 0) + 3);
  
  
          string file = Path.Combine(FileHandler.GetFilePath(ImagePath.Counters), Path.GetFileName(picture));
  
          File.Create(file).Dispose();
  
          using (WebClient client = new WebClient())
          {
              client.DownloadFile(picture, file);
          }
          result.HeroIcon = file;
  
          result.HeroName = Path.GetFileNameWithoutExtension(file).ToLower();
          result.HeroName = result.HeroName.Substring(0, 1).ToUpper() + result.HeroName.Substring(1);
  
          return result;
      }
  </code></pre>
  
  <p>GetFilePath gets me a local path:</p>
  
  <pre><code>    public  static string GetFilePath(ImagePath iP)
      {
          string result = Path.Combine(Directory.GetCurrentDirectory(), "Images", iP.ToString());
          if (!Directory.Exists(result))
              Directory.CreateDirectory(result);
  
          return result;
      }
  </code></pre>
  
  <p>Since this is just a programm I made for my pleasure, its obviously not very proper coded. I create and delete the directory to often for exmaple.</p>