2013-10-01 82 views
0

即時嘗試更新綁定到實現INotifyPropertyChanged的類的圖像控件中的圖像。我已經嘗試了大部分與刷新位圖緩存有關的方法,以便圖像可以刷新,但似乎沒有一種適用於我的情況。圖像contorl在XAML文件所定義:<Image Source="{Binding Chart}" Margin="0 0 0 0"/> 和在類之後的代碼是:更新圖像控件中的圖像

private ImageSource imagechart = null; 

    public ImageSource Chart 
    { 
     get 
     { 
      return imagechart; 
     } 
     set 
     { 
      if (value != imagechart) 
      { 
       imagechart = value; 
       NotifyPropertyChanged("Chart"); 

      } 

     } 
    } 

一個事件後,我現在用下面的代碼設定的圖像:

c.Chart = image; 

時我現在運行我的應用程序,這將顯示圖像,但在應用程序運行期間,我更新圖像,但調用這個c.Chart = image;顯示初始圖像。我開始明白,WPF緩存圖像,但所有方法聲稱爲我解決這個辛苦工作。其中一個解決方案對我不起作用Problems overwriting (re-saving) image when it was set as image source

+0

嘗試進行綁定TwoWay:Image Source =「{Binding Chart,Mode = TwoWay}」 – thumbmunkeys

+0

也沒有工作..我希望圖像控件刷新圖像,而不必關閉應用程序,因爲當我重新運行該應用的圖像將顯示爲更新 –

+0

只是問....您的應用上的所有其他綁定是否按預期工作? –

回答

0

非常感謝大家對我們的投入,因爲我終於想到了解決這個問題的方法。所以我的XAML仍然綁定爲<Image Source="{Binding Chart}" Margin="0 0 0 0"/>,但在後面的代碼,我改變了類屬性圖表返回一個位圖,如下圖所示:

private BitmapImage image = null; 

    public BitmapImage Chart 
    { 
     get 
     { 
      return image; 
     } 
     set 
     { 
      if (value != image) 
      { 
       image = value; 
       NotifyPropertyChanged("Chart"); 

      } 

     } 
    } 

此類提醒你實現INotifyPropertyChanged。在我設置圖像的位置,我現在使用此代碼:

BitmapImage img = new BitmapImage(); 
img.BeginInit(); 
img.CacheOption = BitmapCacheOption.OnLoad; 
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache; 
//in the following code path is a string where i have defined the path to file 
img.UriSource = new Uri(string.Format("file://{0}",path)); 
img.EndInit(); 
c.Chart = img; 

這對我很好,刷新更新後的圖像。

0

嘗試將您的Image屬性的返回類型更改爲Uri。源屬性上的TypeConverter應該完成剩下的工作。如果這不起作用,請確認資源實際上已更改。

您可以使用Assembly.GetManifestResourceStreams從您的程序集中讀取資源並解析字節。比手動將它們保存File.WriteAllBytes到您的輸出目錄,看它是否有預期的圖像。

就我所知,Application Ressources(嵌入到程序集中)不能在運行時(?)期間更改。您正在引用程序集資源,而不是使用包uri的輸出資源。

+0

謝謝你給了我一個想法,並改變了屬性返回一個'BitmapImage'.check我剛剛發佈的答案更多的細節。 –