Darf Zon
Darf Zon

Reputation: 6378

When the Image is null, set another default image?

Can you see this tag:

<Image Source="{Binding Image}" Stretch="UniformToFill"/>

But below? What I'm trying to do is, when the Image is null, set another default image. Is this possible?

<!-- Grid-appropriate 250 pixel square item template as seen in the GroupedItemsPage and ItemsPage -->
<DataTemplate x:Key="Standard250x250ItemTemplate">
    <Grid HorizontalAlignment="Left" Width="320" Height="240">
        <Border Background="{StaticResource ListViewItemPlaceholderRectBrush}">
            <Image Source="{Binding Image}" Stretch="UniformToFill"/>
        </Border>
        <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundBrush}">
            <TextBlock Text="{Binding ShortTitle}" Foreground="{StaticResource ListViewItemOverlayTextBrush}" Style="{StaticResource TitleTextStyle}" Height="48" Margin="15,0,15,0"/>
        </StackPanel>
    </Grid>
</DataTemplate>

Upvotes: 2

Views: 1150

Answers (3)

Mahsa Alamdari
Mahsa Alamdari

Reputation: 62

Simply add two images

<Grid>
    <Image Source="../images/default.jpg"/>
    <Image Source="{Binding ImagePath}"/>
</Grid>

Upvotes: 0

brunnerh
brunnerh

Reputation: 184607

Add a Style with a DataTrigger. e.g. something like

<Image Stretch="UniformToFill">
     <Image.Style>
         <Style TargetType="Image">
             <Setter Property="Source" Value="{Binding Image}"/>
             <Style.Triggers>
                 <DataTrigger Binding="{Binding Image}" Value="{x:Null}">
                     <Setter Property="Source" Value="Images/Default.png"/>
                 </DataTrigger>
             </Style.Triggers>
         </Style>
    </Image.Style>
</Image>

Watch out for precedence of local values.

Upvotes: 2

Erick
Erick

Reputation: 486

I'm not sure that you need a trigger I typically use a fallback value like below when I need to specifiy a value when the bound value will not convert.

<Image Source="{Binding Path=ImageURI, FallbackValue='SomeImageURI'}" />

Microsoft FallbackValue Property

Upvotes: 0

Related Questions