11

我正在尝试设置 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" />

转换器中外部图像的路径看起来像这样,当 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));
4

2 回答 2

33

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

您需要将其设置为 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}}" />

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

于 2013-08-27T06:01:29.480 回答
0

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

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

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;
    }
}
于 2013-06-27T08:07:35.323 回答