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

后端 未结 8 1822
无人及你
无人及你 2021-02-05 09:00

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


But this ImageUri can

相关标签:
8条回答
  • 2021-02-05 09:34

    You can actually go the other way around which is a better experience in my opinion by simply using two Image controls in a Grid layout, 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.

    <Grid>
        <Image Source="Assets/Placeholder.png"/>
        <Image Source="{Binding ImageUri}"/>
    </Grid>
    
    0 讨论(0)
  • 2021-02-05 09:36

    You can set the ImageFailed event on your image,

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

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

    private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
        ((Image)sender).Source = new BitmapImage(new Uri("/Assets/MyDefaultImage.png", UriKind.Relative));
    }
    
    0 讨论(0)
提交回复
热议问题