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

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

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


But this ImageUri can

8条回答
  •  萌比男神i
    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

                
    
                        
    
                    
                               x
                              / \ 
                               |__ (BINDING IMAGE SOURCE WITH NAME)
    
    
                
    

    C# code

    mimg.Source = new BitmapImage(new Uri(dr.GetString("Photo_path"))); 
    

提交回复
热议问题