How to set a default source for an Image if binding source is null?

user2715606 picture user2715606 · Aug 31, 2013 · Viewed 18.2k times · Source

I'm using binding for source of an Image control.

<Image Source="{Binding ImageUri}"/>

But this ImageUri can be null, therefor I want to use a default image, a place holder, for that, which can be in /Assets/PlaceHolder.png for example.

How can I set a default image? thanks. (It's a WP8 app, but should not be different of WPF)

Answer

Nitesh picture Nitesh · Aug 31, 2013

You can achieve it by setting TargetNullValue

<Image>
    <Image.Source>
        <Binding Path="ImageUri" >
            <Binding.TargetNullValue>
                <ImageSource>/Assets/PlaceHolder.png</ImageSource>
            </Binding.TargetNullValue>
        </Binding>
    </Image.Source>
</Image>