Can please any one show me some example on web or explain Image updating in WPF MVVM C#.
My XAML code:
<Image Source="{Binding PicturePath}" VerticalAlignment="Center" Margin="0,0,10,0"/>
C# code
private string _picturepath;
public string PicturePath
{
get { return _picturepath; }
set
{
_picturepath = value;
NotifyPropertyChanged("PicturePath");
}
}
PicturePath = IconPath + @"\Logo.png";
And when picture change, I update PicturePath, but Image in program stay the same. What I need more?
You need to implement the interface INotifyPropertyChanged, add this code, then call OnPropertyChanged() from the setter of PicturePath.
Here's what I ended up with in my project (and I've got to say it took quite a while testing different things that seemed to almost work). You can see the commented code which was also not working 100% don't remember why)
So all my Images from app assets that are starting with "ms-appx:" I use with setting the source without loading to stream, because these never change (default images etc)
The other images that are created or changed by the user I had to reload and set the source with the result of the file read (otherwise when they were changed sometimes they were not updating)
So basically I use this converter on almost all of the places that I use images that can change (without changing their name).
define your converter:
And then use like this
(Another workaround is to name your images differently and then when you update the source path it works fine.)
So now I made stand alone procjet only for picture binding
ImageConverter code:
MainWindowViewModel:
MainWindow:
In my program I have some problem, so I made this standalone project to be sure that works.
Thanks for all the help!