After I click on the listbox to the left where it says "Vienna Tour"; on the right it should show an image. I have binded the image source the following way:
<Image Grid.Row="1" Source="{Binding (vm:MainViewModel.AddTourViewModel).SelectedTour.RouteImage, Converter={StaticResource AddTourViewModel}}"/>
The problem I have now is, the image path is correct but in the UI the image itself is not showing.
This is how I convert the image in my viewmodel:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null
? new BitmapImage() { UriSource = new Uri(value.ToString(), UriKind.Relative) }
: new BitmapImage() { UriSource = new Uri("error.jpg", UriKind.Relative) };
}
The value after clicking "Vienna Tour" points to the image path as you can see on the following picture:
BitmapImage implements the ISupportInitialize
interface, which means that when you initialize a BitmapImage by settings its UriSource
property, you have to call the BeginInit()
and EndInit()
methods.
For convenience, there is a constructor with an Uri
parameter:
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null
? new BitmapImage(new Uri(value.ToString(), UriKind.Relative))
: new BitmapImage(new Uri(@"error.jpg", UriKind.Relative));
}
Alternatively, use the BitmapFrame class:
public object Convert(
object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null
? BitmapFrame.Create(new Uri(value.ToString(), UriKind.Relative))
: BitmapFrame.Create(new Uri(@"error.jpg", UriKind.Relative));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.