h__
h__

Reputation: 793

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));

Upvotes: 11

Views: 13282

Answers (2)

johnson
johnson

Reputation: 388

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.

Upvotes: 32

Anatoliy Nikolaev
Anatoliy Nikolaev

Reputation: 22702

For myself, I created the following example:

<!-- xmlns:sys="clr-namespace:System;assembly=mscorlib" -->

<Window.Resources>
    <!-- Test data -->
    <local:TestDataForImage x:Key="MyTestData" />

    <!-- Image for FallbackValue -->
    <sys:String x:Key="ErrorImage">pack://application:,,,/NotFound.png</sys:String>

    <!-- Image for NULL value -->
    <sys:String x:Key="NullImage">pack://application:,,,/NullImage.png</sys:String>
</Window.Resources>

<!-- Grid using DataContext -->
<Grid DataContext="{StaticResource MyTestData}">
    <Image Name="ImageNull" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Bottom" Source="{Binding Path=NullString, TargetNullValue={StaticResource NullImage}}" />

    <Image Name="ImageNotFound" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Path=NotFoundString, FallbackValue={StaticResource ErrorImage}}" />
</Grid>

The path to the images, I announced in resources. Images are stored in the root of the project. Listing of MyTestData:

public class TestDataForImage : DependencyObject
{
    public string NotFoundString
    {
        get
        {
            return (string)GetValue(NotFoundStringProperty);
        }

        set
        {
            SetValue(NotFoundStringProperty, value);
        }
    }

    public static readonly DependencyProperty NotFoundStringProperty = DependencyProperty.Register("NotFoundString", typeof(string), typeof(TestDataForImage), new PropertyMetadata(""));

    public string NullString
    {
        get
        {
            return (string)GetValue(NullStringProperty);
        }

        set
        {
            SetValue(NullStringProperty, value);
        }
    }

    public static readonly DependencyProperty NullStringProperty = DependencyProperty.Register("NullString", typeof(string), typeof(TestDataForImage), new PropertyMetadata(""));

    public TestDataForImage()
    {
        NotFoundString = "pack://application:,,,/NotExistingImage.png";
        NullString = null;
    }
}

Upvotes: 0

Related Questions