i have few button created list, need change 1 of buttons image. trying convert string button. how take string , turn in button can work ?
i have tried this:
button button = (button)this.findname("button_" + list[0].tostring()); button.background = brush;
i error of object reference not set instance of object when call button.background.
edit how have chosen change image of button:
uri resourceuri = new uri("led_on.png", urikind.relative); streamresourceinfo streaminfo = application.getresourcestream(resourceuri); bitmapframe temp = bitmapframe.create(streaminfo.stream); var brush = new imagebrush(); brush.imagesource = temp; button button = (button)findname("button_" + list[0].tostring()); button.background = brush;
i cant grab button name button_list[0].
one possible solution change wpf button image
on click
event (c#) listed below:
listing 1. changing button image on click (xaml)
<button name ="button1" click="button1_click"> <button.template> <controltemplate targettype="button"> <image x:name="image1"> <image.style> <style targettype="{x:type image}"> <setter property="source" value="images/contentimage.png" /> </style> </image.style> </image> </controltemplate> </button.template> </button>
listing 2. c# code behind (button1.click event handler)
private void button1_click(object sender, routedeventargs a) { image _img= button1.template.findname("image1", button1) image; style _imgstyle = new style { targettype = typeof(image) }; _imgstyle.setters.add(new setter(image.sourceproperty, new bitmapimage(new uri(@"pack://application:,,,/yourassemblyname;component//images/contentimage1.png")))); _img.style = _imgstyle; }
pertinent code, re-written in simple form:
streamresourceinfo _streaminfo = application.getresourcestream(new uri("led_on.png", urikind.relative)); button.background = (new imagebrush(){imagesource=bitmapframe.create(_streaminfo.stream)});
just make sure button
object not null (check statement: "button_" + list[0]
- may list[0]?)
hope may help.
Comments
Post a Comment