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.