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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17316892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:05:09  来源:igfitidea点击:

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

c#wpfbindingconverterfallbackvalue

提问by h__

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.

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

回答by coldjokelife

For the Image control, when you Binding the Source property with a URI string, it will automatically convert the URI to a BitmapImage. But if you set the FallbackValue and TargetNullValue as URI string, it will not display.

对于 Image 控件,当您将 Source 属性与 URI 字符串绑定时,它会自动将 URI 转换为 BitmapImage。但是如果将 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 时,它​​起作用了。

回答by Anatoliy Nikolaev

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