2013-06-26 33 views
4

我想设置FallbackValue的情况下,当我的转换器不能打电话,但我不知道如何做到这一点。在转换器外部图像的如何将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)); 
+0

而不是“Pictures/Unknown.png”指定完整路径 –

+0

是的,它只有在文件存在时才解决问题。如果我保留未知文件并将其包含到可执行文件中,并且使用Uri(“/ Pic/nam.png”)访问它,并且没有使用FallbackValue的相同文件的相对路径访问,那么它有什么区别? –

回答

20

Image控件,当你与一个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的的静态资源, 它的工作原理。

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; 
    } 
} 
相关问题