【问题标题】:How to set a default source for an Image if binding source is null?如果绑定源为空,如何为图像设置默认源?
【发布时间】:2013-09-03 19:01:46
【问题描述】:

我正在为Image 控件的源使用绑定。

<Image Source="{Binding ImageUri}"/>

但是这个ImageUri 可以为空,因此我想使用默认图像,占位符,例如可以在/Assets/PlaceHolder.png 中。

如何设置默认图片?谢谢。 (它是一个 WP8 应用程序,但不应与 WPF 不同)

【问题讨论】:

    标签: wpf xaml windows-phone-7 windows-phone-8


    【解决方案1】:

    可以通过设置TargetNullValue来实现

    <Image>
        <Image.Source>
            <Binding Path="ImageUri" >
                <Binding.TargetNullValue>
                    <ImageSource>/Assets/PlaceHolder.png</ImageSource>
                </Binding.TargetNullValue>
            </Binding>
        </Image.Source>
    </Image>
    

    【讨论】:

    • 我有一个应用程序,它可以加载数百张图像。如何在加载实际图像之前显示默认图像?
    • @ArjunKR 您可能想访问stackoverflow.com/questions/19113141/…
    • 太棒了! WPF 从简单的优雅转向令人沮丧的冗长,总是让我感到惊讶。
    • 只是在不明显的情况下添加,“ImageUri”是 Image 控件的数据上下文属性“ImageUri”的绑定路径,可以更改为适当的路径 - 它不是图像源。我还必须将我的 ImageSource 指定为程序集 uri 才能工作:/AssemblyName;component/Images/Placeholder.png
    【解决方案2】:

    实际上,您可以换一种方式,在我看来,这是一种更好的体验,只需在 Grid 布局中使用两个 Image 控件,一个使用本地占位符源,一个使用远程 Binding。当您的远程绑定为空时,本地图像已经存在。但是,如果绑定不为空,它会在渲染后自动覆盖本地占位符图像。

    <Grid>
        <Image Source="Assets/Placeholder.png"/>
        <Image Source="{Binding ImageUri}"/>
    </Grid>
    

    【讨论】:

    • 理论上可行,但我的图像具有透明背景。这意味着我可以在我的其他图像下方看到图像。如何隐藏下面的图像?
    • @Sweepster 尝试使用绑定图像的 ImageOpened 事件。如果数据绑定事件的图片打开事件成功触发,则设置占位符图片的不透明度为0!!
    【解决方案3】:

    你可以在你的图片上设置 ImageFailed 事件,

    <Image Source="{Binding ImageUri}" ImageFailed="Image_ImageFailed"/>
    

    并使用以下 C# 在其位置加载特定图像。

    private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
    {
        ((Image)sender).Source = new BitmapImage(new Uri("/Assets/MyDefaultImage.png", UriKind.Relative));
    }
    

    【讨论】:

      【解决方案4】:

      你可以试试这个:

      <Image>
          <Image.Source>
              <Binding Path="ImageUri">
                  <Binding.TargetNullValue>
                      <BitmapImage UriSource="/ProjName;component/Assets/PlaceHolder.png" />                    
                  </Binding.TargetNullValue>
              </Binding>
          </Image.Source>
      </Image>
      

      【讨论】:

        【解决方案5】:

        您可以使用 ImageFailed 事件和 ChangePropertyAction

        这段代码片段对我有用:

        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>
        

        【讨论】:

        • 我有一个应用程序,它可以加载数百张图像。如何在加载实际图像之前显示默认图像?
        【解决方案6】:

        使用TargetNullValue 属性。如果我不想显示任何图像,这将非常有帮助。

        【讨论】:

          【解决方案7】:

          您应该在这里尝试使用FallBackValue。这两个链接应该提供更多帮助

          WPF Binding - Default value for empty string

          MSDN

          【讨论】:

            猜你喜欢
            • 2017-12-03
            • 1970-01-01
            • 2022-12-13
            • 2017-03-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-25
            • 2013-11-24
            相关资源
            最近更新 更多