2011-04-24 63 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