首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当图像为空时,是否设置另一个默认图像?

当图像为空时,是否设置另一个默认图像?
EN

Stack Overflow用户
提问于 2012-04-08 11:06:29
回答 2查看 1.1K关注 0票数 1

你能看到这个标签吗:

代码语言:javascript
复制
<Image Source="{Binding Image}" Stretch="UniformToFill"/>

但是下面呢?我尝试做的是,当image为空时,设置另一个默认图像。这个是可能的吗?

代码语言:javascript
复制
<!-- 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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-08 11:11:47

添加具有DataTriggerStyle。例如像这样的

代码语言:javascript
复制
<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>

注意本地值precedence

票数 2
EN

Stack Overflow用户

发布于 2012-04-09 22:09:30

我不确定你是否需要触发器,当我需要指定一个值,而绑定值不会进行转换时,我通常会使用如下的回退值。

代码语言:javascript
复制
<Image Source="{Binding Path=ImageURI, FallbackValue='SomeImageURI'}" />

Microsoft FallbackValue Property

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10060068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档