首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将绑定中的FallbackValue设置为外部图像文件的路径?

如何将绑定中的FallbackValue设置为外部图像文件的路径?
EN

Stack Overflow用户
提问于 2013-06-26 17:53:44
回答 2查看 10.5K关注 0票数 11

我正在尝试设置FallbackValue,以防我的转换器无法被调用,但我不确定该如何做。

代码语言:javascript
复制
<Image Source="{Binding FallbackValue="Pictures/Unknown.png", Path=LatestPosition.DeviceFamily, Converter={x:Static conv:ConverterSet.DeviceTypeToImageSourceconverter}}" Name="image1" Stretch="Fill" Margin="5,8" Width="150" Height="150" Grid.RowSpan="4" />

转换器中外部图像的路径看起来就像这样,当LatestPosition!=null时,图像以正确的方式设置。

代码语言:javascript
复制
private static readonly ImageSource Dev1 = new BitmapImage(new Uri("/Pictures/dev1.png", UriKind.Relative));
private static readonly ImageSource Dev2 = new BitmapImage(new Uri("/Pictures/dev2.png", UriKind.Relative));
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-27 14:01:29

对于Image控件,当您将Source属性与URI字符串绑定时,它会自动将URI转换为BitmapImage。但是如果您将FallbackValue和TargetNullValue设置为URI字符串,它将不会显示。

您需要将其设置为BitmapImage:

代码语言:javascript
复制
<Window.Resources>
    <BitmapImage x:Key="DefaultImage" UriSource="/Resources;component/Images/Default.jpg" />
</Window.Resources>

<Image Width="128"
               Height="128"
               HorizontalAlignment="Left"
               VerticalAlignment="Top"
               Source="{Binding Photo,FallbackValue={StaticResource DefaultImage},
                                TargetNullValue={StaticResource DefaultImage}}" />

当我们将FallbackValue和TargetNullValue设置为BitmapImage的StaticResource时,它就可以工作了。

票数 33
EN

Stack Overflow用户

发布于 2013-06-27 16:07:35

对于我自己,我创建了以下示例:

代码语言:javascript
复制
<!-- xmlns:sys="clr-namespace:System;assembly=mscorlib" -->

<Window.Resources>
    <!-- Test data -->
    <local:TestDataForImage x:Key="MyTestData" />

    <!-- Image for FallbackValue -->
    <sys:String x:Key="ErrorImage">pack://application:,,,/NotFound.png</sys:String>

    <!-- Image for NULL value -->
    <sys:String x:Key="NullImage">pack://application:,,,/NullImage.png</sys:String>
</Window.Resources>

<!-- Grid using DataContext -->
<Grid DataContext="{StaticResource MyTestData}">
    <Image Name="ImageNull" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Bottom" Source="{Binding Path=NullString, TargetNullValue={StaticResource NullImage}}" />

    <Image Name="ImageNotFound" Width="100" Height="100" HorizontalAlignment="Left" VerticalAlignment="Top" Source="{Binding Path=NotFoundString, FallbackValue={StaticResource ErrorImage}}" />
</Grid>

通向图像的路径,我在参考资料中宣布。图像存储在项目的根目录中。MyTestData列表

代码语言:javascript
复制
public class TestDataForImage : DependencyObject
{
    public string NotFoundString
    {
        get
        {
            return (string)GetValue(NotFoundStringProperty);
        }

        set
        {
            SetValue(NotFoundStringProperty, value);
        }
    }

    public static readonly DependencyProperty NotFoundStringProperty = DependencyProperty.Register("NotFoundString", typeof(string), typeof(TestDataForImage), new PropertyMetadata(""));

    public string NullString
    {
        get
        {
            return (string)GetValue(NullStringProperty);
        }

        set
        {
            SetValue(NullStringProperty, value);
        }
    }

    public static readonly DependencyProperty NullStringProperty = DependencyProperty.Register("NullString", typeof(string), typeof(TestDataForImage), new PropertyMetadata(""));

    public TestDataForImage()
    {
        NotFoundString = "pack://application:,,,/NotExistingImage.png";
        NullString = null;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17316892

复制
相关文章

相似问题

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