HOW TO SET A DEFAULT VALUE ON A DATATEMPLATE?

ezra heywood

Total Post:145

Points:1019
Posted by  ezra heywood
WPF 
WPF
 1024  View(s)
Ratings:
Rate this:
Hi Mindstick!

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>

Thanks in advance

  1. shreesh chandra shukla

    Post:105

    Points:735
    Re: How to set a default value on a DataTemplate?

    Hi!

    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>

    thanks

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!