2013-06-26 55 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; 
    } 
} 
相關問題