I\'m using binding for source of an Image
control.
But this ImageUri
can
You should try to play with FallBackValue
here. These two links should provide more help
WPF Binding - Default value for empty string
MSDN
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>
use TargetNullValue
attribute. It is very helpful if I don't want to display any image.
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")));
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>
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>