0

I am trying to display an image with a URL source in my MVVM WPF application. The Xaml I use is:

<ContentControl>
    <Image Margin="5" Height="190" Source="{Binding CampaignMainImage}"/>
</ContentControl>

In my view model, I tried binding this with both CampaignMainImage as string and as BitmapImage. In both of these cases, the image source is from a web URL. Both of these worked, and my application displayed the image successfully.

However, in the same application, I need to allow the users to upload a new image. I do this with file selection and ftp upload. I do the upload successfully, and see the breakpoint hit where PropertyChanged event is raised.

The problem is, the new image's name must be the same with the old one and WPF seems to be caching the image. Even when I close that window in the application and new up an instance of the viewmodel, still the old image is displayed. When I close the application and run again, the new image is displayed.

How can I stop this caching behavior?

Edit:

The proposed duplicate solves the issue with binding to a BitmapImage (a view related type). As indicated by Tseng in the comments below, this is a non-MVVM solution where databinding to view related types do not break the model. The question, to put it more specifically, is how can this caching behavior be done without breaking the MVVM pattern.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
A. Burak Erbora
  • 1,054
  • 2
  • 12
  • 26
  • 2
    It's already answered here: http://stackoverflow.com/questions/4137469/wpf-image-source-caching-too-aggressively – drcolombo Jan 18 '16 at 08:30
  • Thank you so much. I've been searching for this for a while and somehow did not see that. – A. Burak Erbora Jan 18 '16 at 08:38
  • This question isn't exactly a duplicate. This question is tagged with MVVM, the linked question is just about WPF. Using `BitmapImage` in the ViewModel is a violation of the MVVM pattern – Tseng Jan 18 '16 at 10:31
  • 2
    You can do this in xaml right in the view, or you can do it in the codebehind of the UI. Either is perfectly fine in MVVM. Another alternative is to do your image downloading in your VM, then expose the image as a byte array. –  Jan 18 '16 at 16:48
  • It seems to me that if you want a "MVVM" solution, you need to first explain _specifically_ in what way you feel the marked duplicate "is a violation of the MVVM pattern". Note that you don't have to put a `BitmapImage` in your actual view model, so the fact that you might want to use one doesn't violate MVVM per se. Also, please do not put tags in the title. – Peter Duniho Jan 23 '16 at 05:26

1 Answers1

0

This question has actually been asked and answered (thanks to drcolombo for pointing out). But (as pointed out by tseng), the answer does not pertain to the MVVM pattern and breaks the pattern by usage of a view typed element (BitmapImage) in the viewmodel.

For those interested in a non-MVVM answer, the link for the prior question can be found here.

The non-MVVM solution is to set

bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache

when creating the bitmap image.

Community
  • 1
  • 1
A. Burak Erbora
  • 1,054
  • 2
  • 12
  • 26
  • 1
    See my comment from above. Using this violates your MVVM pattern approach. You can't databind to `BitmapImage`, because it's a view related type (defined in PresentationCore.dll, this is an assembly specific to WPF). Using it in your Viewmodel creates a tight coupling to WPF and this is what you want to avoid with MVVM – Tseng Jan 18 '16 at 10:37
  • Thanks for pointing this out. I've edited the question and the answer and voted for the question to re-open. – A. Burak Erbora Jan 18 '16 at 11:42