2011-04-24 44 views
0

我正在開發Windows Phone應用程序。Imagebutton更改源屬性

我有一個自定義按鈕,裏面有圖像。這是它的XAML代碼:

<ControlTemplate x:Key="ImageButton" TargetType="Button"> 
    <Grid> 
     <Image Margin="45,8,35,8" Source="Images/Delete.png"/> 
    </Grid> 
</ControlTemplate> 

如何以編程方式更改Image Source屬性?

回答

5

要改變圖像的來源,你需要讓你的資產的新位圖,並將其設置爲烴源

BitmapImage myBitmapImage = new BitmapImage(new Uri("/Images/foo.png", UriKind.Relative)); 

imageButton.Source = myBitmapImage; 

我猜你想有一個按鈕,改變它的背景如果被點擊。您需要重新定義按鈕的視覺狀態。這裏是一個例子:

Windows Phone 7 (WP7) Change a button's background color on click