xaml - how to change button background color in c# -
how can change button background color programatically in windows phone application. here xaml code.
<style targettype="button" x:key="tabbuttonlast"> <setter property="foreground" value="navy"/> <setter property="background" value="green" /> <setter property="template"> <setter.value> <controltemplate targettype="button"> <border cornerradius="15,15,15,15" background="green" > <contentpresenter x:name="contentpresenter" horizontalalignment="center" verticalalignment="center"/> </border> </controltemplate> </setter.value> </setter> </style> <button name="btnnext" style="{staticresource tabbuttonlast}" content="next" height="23" horizontalalignment="left" margin="131,311,0,0" verticalalignment="top" width="75" click="btnnext_click" />
i tried using "using system.drawing" yourbuttonname.backcolor = color.red; doesn't seem work.any appreciated.
you need modify style follows:
<style targettype="button" x:key="tabbuttonlast"> <setter property="foreground" value="navy"/> <setter property="background" value="{binding background, relativesource={relativesource self}}"/> <setter property="template"> <setter.value> <controltemplate targettype="button"> <border cornerradius="15,15,15,15" background="{templatebinding background}"> <contentpresenter x:name="contentpresenter" horizontalalignment="center" verticalalignment="center"/> </border> </controltemplate> </setter.value> </setter> </style>
1) if want static background:
<button background="red" name="btnnext" style="{staticresource tabbuttonlast}" content="next" height="23" horizontalalignment="left" margin="131,311,0,0" verticalalignment="top" width="75" click="btnnext_click" />
2) change backcolor code:
private void changebuttoncolor() { btnnext.background = "red"; }
3) using mvvm approach example:
"frontend":
<window x:class="wpfapplication3.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpfapplication3" mc:ignorable="d" title="mainwindow" height="350" width="525"> <window.resources> <style targettype="button" x:key="tabbuttonlast"> <setter property="foreground" value="navy"/> <setter property="background" value="{binding background, relativesource={relativesource self}}"/> <setter property="template"> <setter.value> <controltemplate targettype="button"> <border cornerradius="15,15,15,15" background="{templatebinding background}"> <contentpresenter x:name="contentpresenter" horizontalalignment="center" verticalalignment="center"/> </border> </controltemplate> </setter.value> </setter> </style> </window.resources> <grid> <button style="{staticresource tabbuttonlast}" content="change color" background="{binding btnbackcolor}" margin="50" horizontalalignment="stretch" verticalalignment="stretch" click="button_click" /> </grid> </window>
"backend":
using system; using system.componentmodel; using system.runtime.compilerservices; using system.windows; using system.windows.media; namespace wpfapplication3 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window, inotifypropertychanged { public brush btnbackcolor { get; set; } = new solidcolorbrush(colors.red); public mainwindow() { initializecomponent(); this.datacontext = this; } public event propertychangedeventhandler propertychanged; private void button_click(object sender, routedeventargs e) { random r = new random(); //without binding variant //btnnext.background = new solidcolorbrush(color.fromrgb((byte)r.next(1, 255), // (byte)r.next(1, 255), (byte)r.next(1, 233))); //mvvm approach variant btnbackcolor = new solidcolorbrush(color.fromrgb((byte)r.next(1, 255), (byte)r.next(1, 255), (byte)r.next(1, 233))); onpropertychanged("btnbackcolor"); } protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } } }
smth should work...
Comments
Post a Comment