Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set FallbackValue in binding as path to external image file?

I'm trying to set FallbackValue in case when my converter cannot be call, but I'm not sure how to do that.

<Image Source="{Binding FallbackValue="Pictures/Unknown.png", Path=LatestPosition.DeviceFamily, Converter={x:Static conv:ConverterSet.DeviceTypeToImageSourceconverter}}" Name="image1" Stretch="Fill" Margin="5,8" Width="150" Height="150" Grid.RowSpan="4" />

Paths of external images in converter looks like that and when LatestPosition!=null the image is set in proper way.

private static readonly ImageSource Dev1 = new BitmapImage(new Uri("/Pictures/dev1.png", UriKind.Relative));
private static readonly ImageSource Dev2 = new BitmapImage(new Uri("/Pictures/dev2.png", UriKind.Relative));
like image 540
h__ Avatar asked Jun 26 '13 09:06

h__


1 Answers

For the Image control, when you Binding the Source property with a URI string, it will automatically convert the URI to a BitmapImage. But if you set the FallbackValue and TargetNullValue as URI string, it will not display.

You need to set it as BitmapImage:

<Window.Resources>
    <BitmapImage x:Key="DefaultImage" UriSource="/Resources;component/Images/Default.jpg" />
</Window.Resources>

<Image Width="128"
               Height="128"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Source="{Binding Photo,FallbackValue={StaticResource DefaultImage},
                                TargetNullValue={StaticResource DefaultImage}}" />

As we set the FallbackValue and the TargetNullValue as StaticResource of BitmapImage, It works.

like image 120
johnson Avatar answered Oct 12 '22 16:10

johnson