如果绑定源为空,如何为图像设置默认源? [英] How to set a default source for an Image if binding source is null?

查看:27
本文介绍了如果绑定源为空,如何为图像设置默认源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Image 控件的源使用绑定.

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

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

但是这个 ImageUri 可以为 null,因此我想使用默认图像,一个占位符,它可以在 /Assets/PlaceHolder.png例如.

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.

如何设置默认图片?谢谢.(它是一个 WP8 应用程序,但应该与 WPF 没有区别)

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

推荐答案

可以通过设置TargetNullValue

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

这篇关于如果绑定源为空,如何为图像设置默认源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆