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

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

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


But this ImageUri can

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

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

    WPF Binding - Default value for empty string

    MSDN

    0 讨论(0)
  • 2021-02-05 09:21

    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>
    
    0 讨论(0)
  • 2021-02-05 09:26

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

    0 讨论(0)
  • 2021-02-05 09:26

    ok, so i know this is a little old , but I think I have the easiest one yet I was searching for the same problem and found out this to work really great actually

    make a grid and grid columns and add two pictures to it, then assign both to the same column, so that they are over each other , don't assign z indexes or anything just add the default image first and then a binding to the images that need to be loaded from the database, that's it done ,now if the image is null the binding source will be transparent and the default image behind will be visible ,

    this is the easiest method if you don't know a lot and less code

    note : better to add a smaller and png image , but works fine with either of those

    xaml code

                <Grid>
    
                    <Image Source="Images/cartoon-woman-Pretty.Png" Stretch="Uniform" 
                    Height="230" Width="180"/>    
    
                    <Image  Stretch="Uniform" x:Name="mimg" Height="230" Width="180" />
                               x
                              / \ 
                               |__ (BINDING IMAGE SOURCE WITH NAME)
    
    
                </Grid>
    

    C# code

    mimg.Source = new BitmapImage(new Uri(dr.GetString("Photo_path"))); 
    
    0 讨论(0)
  • 2021-02-05 09:28

    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>
    
    0 讨论(0)
  • 2021-02-05 09:29

    You can use ImageFailed event and 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>
    
    0 讨论(0)
提交回复
热议问题