2011-11-10 29 views
4

我有這個當前工作爲一個數據綁定WPF圖像:WPF圖像UriSource,數據綁定和緩存

<Image Source="{Binding ThumbFile}" /> 

簡單。

現在,添加緩存到這個圖像(我想能夠操作/刪除本地文件後,已被加載)。我發現你可以添加一個CacheOption =「OnLoad」給標籤中的標籤。

<Image> 
    <Image.Source> 
     <BitmapImage UriSource="{Binding Path=ThumbFile, Converter={StaticResource myConverter2}}" /> 
    </Image.Source> 
</Image> 

然後,我不得不有一個轉換器將本地文件轉換爲BitmapImage。

<local:LocalUriToImageConverter x:Key="myConverter2"/> 

public class LocalUriToImageConverter : System.Windows.Data.IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value == null) 
     { 
      return null; 
     } 

     if (value is string) 
     { 
      value = new Uri((string)value); 
     } 

     if (value is Uri) 
     { 
      System.Windows.Media.Imaging.BitmapImage bi = new System.Windows.Media.Imaging.BitmapImage(); 
      bi.BeginInit(); 
      //bi.DecodePixelWidth = 80; 
      bi.DecodePixelHeight = 60;     
      bi.UriSource = (Uri)value; 
      bi.EndInit(); 
      return bi; 
     } 

     return null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new Exception("The method or operation is not implemented."); 
    } 
} 

出於某種原因,這還沒有開始工作。沒有錯誤,但控制似乎不受約束。即使控件創建了許多實例,ThumbFile屬性和Converter中的斷點也沒有達到。切換回其他圖像源標籤工作正常。

+0

什麼都沒有?失敗的綁定等...? –

+0

什麼也沒有。我經歷了很多不同的錯誤,試圖達到它的效果,但最終在實際工作中終止了錯誤。 – elbweb

回答

1

我無法從代碼中知道發生了什麼,但我會使用Snoop深入瞭解它並查看發生了什麼。您應該能夠看到任何綁定錯誤並查看在Image上,並確保您的DataContext上的ThumbFile屬性具有您所期望的。

0

我遇到了同樣的問題,並且在BitmapImage的UriSource屬性上使用時,從來沒有找到一種方法使任何轉換器的綁定工作。我認爲這不是用這種方式來使用的。

不過,我相信下面的代碼是等同,並應工作你的情況(在我的工作):在你的輸出窗口

<Image Source="{Binding ThumbFile, Converter={StaticResource myConverter2}}" />