I have a Listbox in which every element has an image that is already stored as content. I choose the image to be displayed using a converter.
If the image for the corresponding value does not exist, I have to display a default image which I have handled in the ImageFailed event.
The problem is that when I run the program I am getting the default image for a few images that already exist. If I scroll down the list box and back up again sometimes an image which was displayed properly displays the default image. This seems to be an performance issue.
I am new to application development, Let me know any detail even though it might seem trivial to you.
Below is my implementation
<ListBox DataContext="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Image Width="90" Height="67" Source="{Binding id,Converter={StaticResource imageConverter}}" ImageFailed="ImageFailed" />
_
_
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The convert function
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string Id = (string)value;
string imagePath;
imagePath = string.Format(AppDefines.channelLogoImgPath, prgSvcId);
return imagePath;
}
The ImageFailed handler
private void ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
Image Img = (Image)sender;
string imgPath = Defines.defImagePath
Uri uri = new Uri(imgPath, UriKind.RelativeOrAbsolute);
BitmapImage bDefImage = new BitmapImage(uri);
Img.Source = bDefImage;
}
The problem is that your convertor is returning a string (path to the image) and not an ImageSource.
You need something like this instead:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string imagePath = string.Format(AppDefines.channelLogoImgPath, value);
return new BitmapImage(imagePath);
}
As Matt noted, you are also not using the id you are passing to the convertor. Simplified code above includes that fix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With