c# - How to change the background image of a button when clicked in code? -


there similar question mine here in stackoverflow explains how change in xaml. want know how can change in code.

here image shows how in xaml using blend:

image

link full size: https://snag.gy/4skk4.jpg

basically want change background of button's pressed state in c# can't seem find examples on internet. must in code because image of button change therefore button's pressed image must change well.

the following code change image of button , it's start.

image.imagesource = new system.windows.media.imaging.bitmapimage(new uri(@"images/button-warning-icon.png", urikind.relative)); image.stretch = stretch.uniform; buttonwarnings.background = image; 

if understand correctly, trying change appearance of button control in "pressed" visual state.

i'm not near dev computer try out, "unblock you" i'll give direction.

first, noticed in blend screenshot, each visual state represented storyboard, defines how various properties change. in case, you're looking change background property.

the visualstategroups , states defined control. can override them when re-template control. so, retemplate button control using blend "edit template"->"edit copy".

then, in code, should able following:

1) visual states (this not work unless re-template control, afaik)

var visualstategroups = visualstatemanager.getvisualstategroups(buttonwarnings); 

2) visualstategroup of "commonstates" visualstategroups collection

var commonstatesgroup = visualstategroups.find((g) => ((visualstategroup)g).name == "commonstates") visualstategroup; 

3) "pressed" visualstate:

var pressedvisualstate = commonstatesgroup.find((vs) => ((visualstate)vs).name == "pressed") visualstate; 

4) change storyboard of state

pressedvisualstate.storyboard = newstoryboardwithcustomimagebackgroundproperty; 

(disclaimer: i'm not near in computer try - it's in theory)


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -