wpf 如果绑定源为空,如何为图像设置默认源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18543235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:32:50  来源:igfitidea点击:

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

wpfxamlwindows-phone-7windows-phone-8

提问by user2715606

I'm using binding for source of an Imagecontrol.

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

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

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

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

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

如何设置默认图像?谢谢。 (它是一个 WP8 应用程序,但不应与 WPF 不同)

回答by Nitesh

You can achieve it by setting TargetNullValue

您可以通过设置来实现 TargetNullValue

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

回答by ZombieSheep

You can set the ImageFailed event on your image,

您可以在图像上设置 ImageFailed 事件,

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

and use the following C# to load a specific image in its place.

并使用以下 C# 在其位置加载特定图像。

private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
    ((Image)sender).Source = new BitmapImage(new Uri("/Assets/MyDefaultImage.png", UriKind.Relative));
}

回答by Shikhar

You can actually go the other way around which is a better experience in my opinion by simply using two Imagecontrols in a Gridlayout, one with the local placeholder source and one with the remote Binding. The local image is already there when you your remote binding is null. However if the binding is not null, it automatically covers the local placeholder image once it gets rendered.

实际上,您可以通过ImageGrid布局中简单地使用两个控件(一个带有本地占位符源,一个带有远程Binding. 当您的远程绑定为空时,本地图像已经存在。但是,如果绑定不为空,它会在渲染后自动覆盖本地占位符图像。

<Grid>
    <Image Source="Assets/Placeholder.png"/>
    <Image Source="{Binding ImageUri}"/>
</Grid>

回答by Hossein Narimani Rad

You may try this:

你可以试试这个:

<Image>
    <Image.Source>
        <Binding Path="ImageUri">
            <Binding.TargetNullValue>
                <BitmapImage UriSource="/ProjName;component/Assets/PlaceHolder.png" />                    
            </Binding.TargetNullValue>
        </Binding>
    </Image.Source>
</Image>

回答by SeyedPooya Soofbaf

You can use ImageFailedevent and ChangePropertyAction.

您可以使用ImageFailed事件和ChangePropertyAction

This Code Snippet worked for me:

此代码片段对我有用:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

<Image x:Name="GeologyMapsLegend" Stretch="Fill" Height="150">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="ImageFailed">
            <ei:ChangePropertyAction PropertyName="Source" TargetName="GeologyMapsLegend">
                <ei:ChangePropertyAction.Value>
                    <ImageSource>
                        /LanSysWebGIS;component/Pictures/Icon/NoImageAvailable.jpg
                    </ImageSource>
                </ei:ChangePropertyAction.Value>
            </ei:ChangePropertyAction>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Image>

回答by Muhammad Ammar

use TargetNullValueattribute. It is very helpful if I don't want to display any image.

使用TargetNullValue属性。如果我不想显示任何图像,这将非常有帮助。

回答by AsitK

You should try to play with FallBackValuehere. These two links should provide more help

你应该试着在FallBackValue这里玩。这两个链接应该会提供更多帮助

WPF Binding - Default value for empty string

WPF 绑定 - 空字符串的默认值

MSDN

MSDN