Imagebutton change source property

别等时光非礼了梦想. 提交于 2019-12-11 17:09:18

问题


I'm developing a Windows Phone app.

I have a custom button with an image inside. This is its XAML code:

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

How can I change Image Source property programmatically?


回答1:


To change the source of an image you need to make a new bitmap of your asset and set it as a souce

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

imageButton.Source = myBitmapImage;

but I guess you would like to have a button that change its background if is clicked. You need to redefine Visual States of the button. Here is an example:

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



来源:https://stackoverflow.com/questions/5770527/imagebutton-change-source-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!