c# - Changing a button's mouse hover color BASED ON the background color -


i've been trying figure out on how change button's hover color , found this:

but half of i'm trying achieve. want change hover color based on current background color. i.e

default button background color: gray

i click , button's background color become red. hover color should changed pink.

i click again , button's background color become green. hover color should changed light green.

is possible in mark , in code behind? how?

xaml:

<button content="color change" background="{binding background}" command="{binding buttonpressedcommand}">         <button.template>             <controltemplate targettype="button">                 <border name="border" background="{binding relativesource={relativesource templatedparent}, path=background, mode=oneway}">                     <contentpresenter horizontalalignment="center" verticalalignment="center"/>                 </border>                 <controltemplate.triggers>                     <trigger property="ismouseover" value="true">                         <setter property="background" targetname="border"                                  value="{binding backgroundonhover}"/>                     </trigger>                     <trigger property="ismouseover" value="false">                         <setter property="background" targetname="border"                                  value="{binding background}"/>                     </trigger>                 </controltemplate.triggers>             </controltemplate>         </button.template>     </button> 

viewmodel:

class mainviewmodel : inotifypropertychanged     {         private brush _background = new solidcolorbrush(colors.red);          public brush background         {             { return _background; }             set             {                 _background = value;                 onpropertychanged();             }         }          private brush _backgroundonhover;         public brush backgroundonhover         {                         {               if (_backgroundonhover == null)                     sethoverbackground();                 debug.writeline(((solidcolorbrush)_backgroundonhover).color.r);                 return _backgroundonhover;             }             set             {                 _backgroundonhover = value;                 onpropertychanged();             }         }          private relaycommand _buttonpressedcommand;          public relaycommand buttonpressedcommand         {                         {                 return _buttonpressedcommand ??                        (_buttonpressedcommand = new relaycommand(setbackgroundwhenbuttonpressed));             }         }          private void setbackgroundwhenbuttonpressed()         {             var color = ((solidcolorbrush)background).color;             background = new solidcolorbrush(color.fromrgb((byte)(color.r - 5), color.g, color.b));             sethoverbackground();         }          private void sethoverbackground()         {             var color = ((solidcolorbrush)background).color;             backgroundonhover = new solidcolorbrush(color.fromrgb((byte)(255-color.r ), color.g, color.b));         }           public void onpropertychanged([callermembername] string propertyname = null)         {             if (propertychanged != null)                 propertychanged(this, new propertychangedeventargs(propertyname));         }          public event propertychangedeventhandler propertychanged;     } 

whenever click changes background color , simultaneously color when mouse over.


Comments