2011-11-25 74 views
1

如何從.cs文件更改圖像的當前源?我已經試過這樣:從.cs文件更改圖像源

file.xaml.cs:

BitmapImage yesImage = new BitmapImage(new Uri("/Test;component/Images/yes.png")); // new source address 
img.Source = yesImage; // update source of img 

XAML文件有一個控制<image Name="img" />居住在它。

+0

這應該工作 - 假設圖像存在於指定的位置。究竟發生了什麼? – ChrisF

回答

2

我現在工作了。我將圖像的「構建動作」更改爲「內容」,並且由於圖像對於我移除的「/ Test; component /」項目而言是本地的,並且我還向URI對象添加了UriKind.Relative。

BitmapImage yesImage = new BitmapImage(
     new Uri("/Images/yes.png", UriKind.Relative)); // new source address 
img.Source = yesImage; // update source of img