简体   繁体   English

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

[英]How to set FallbackValue in binding as path to external image file?

I'm trying to set FallbackValue in case when my converter cannot be call, but I'm not sure how to do that. 我正在尝试设置FallbackValue以防万一我的转换器无法调用,但我不知道该怎么做。

<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" />

Paths of external images in converter looks like that and when LatestPosition!=null the image is set in proper way. 转换器中外部图像的路径看起来像那样,当LatestPosition!= null时,图像以适当的方式设置。

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));

For the Image control, when you Binding the Source property with a URI string, it will automatically convert the URI to a BitmapImage. 对于Image控件,当您使用URI字符串绑定Source属性时,它会自动将URI转换为BitmapImage。 But if you set the FallbackValue and TargetNullValue as URI string, it will not display. 但是如果将FallbackValue和TargetNullValue设置为URI字符串,则不会显示。

You need to set it as BitmapImage: 您需要将其设置为BitmapImage:

<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}}" />

As we set the FallbackValue and the TargetNullValue as StaticResource of BitmapImage, It works. 当我们将FallbackValue和TargetNullValue设置为BitmapImage的StaticResource时,它可以工作。

For myself, I created the following example: 对于我自己,我创建了以下示例:

<!-- 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>

The path to the images, I announced in resources. 我在资源中宣布了图像的路径。 Images are stored in the root of the project. 图像存储在项目的根目录中。 Listing of MyTestData : MyTestData列表:

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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM