Whenever I wanted to display an image in the past, I bound the image path to the image's source property. Too easy.
Now I want to change the image and always show the image with the latest changes. The change image is saved in a BitmapImage
property inside my class. So instead of asking the image control to load the image from disc, I wanted to bind it directly to my BitmapImage
property. But the image does not show.
Then (just for testing) I created a value converter, used the path to the image in there to create a BitmapImage
and returned that to the control - and the image shows.
Again: Creating a BitmapImage
from a path inside the coverter works. Binding the image control to a BitmapImage
property that is created in my class with the same code fails.
That describes the same problem, but the "solution" doesn't work. (I wonder why it is marked as solved, since the OP made the same comment)
EDIT: here is some code
This is the converter that successfully creates a visible image.
public class BsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
BitmapImage bi = new BitmapImage(new Uri(value.ToString()));
return bi;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
... and the XAML binding that does show the image. File
is of type FileInfo
and FullName
holds the complete path.
<Image MinHeight="100" MinWidth="100" Source="{Binding SelectedImage.File.FullName, Converter={StaticResource BsConverter}}"/>
I have a property BitmapImage image { get; set; }
that I initialize in the constructor of my class:
image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(file.FullName);
image.CacheOption = BitmapCacheOption.OnLoad;
image.EndInit();
...and the binding. But - no joy. The image is not displayed.
<Image MinHeight="100" MinWidth="100" Source="{Binding SelectedImage.image}"/>