xaml - Change the foreground of a button when clicked -


so trying change color of button when clicked i've spent amount errors , couldn't find helpful. understand brand new visual studio 2015 community.

there seems lot of ways either using brushes or solidcolorbrushes or conversion.

please mention using , references needed

here button:

<button x:name="castbutton"         click="castbutton_click"         horizontalalignment="right"         verticalalignment="bottom"         margin="0,0,35,10"         fontfamily="segoe mdl2 assets"         fontsize="24"         foreground="aliceblue"         content="&#xe72d;"> </button> 

if want change foreground color when button pressed, have modify style of control. right click on button in designer window, edit template->edit copy - should create copy of default template in resources, should this:

<page.resources>     <style x:key="mybuttonstyle" targettype="button">         <setter property="background" value="{themeresource systemcontrolbackgroundbaselowbrush}"/>         <setter property="foreground" value="{themeresource systemcontrolforegroundbasehighbrush}"/>         <setter property="borderbrush" value="{themeresource systemcontrolforegroundtransparentbrush}"/>         <setter property="borderthickness" value="{themeresource buttonborderthemethickness}"/>         <setter property="padding" value="8,4,8,4"/>         <setter property="horizontalalignment" value="left"/>         <setter property="verticalalignment" value="center"/>         <setter property="fontfamily" value="{themeresource contentcontrolthemefontfamily}"/>         <setter property="fontweight" value="normal"/>         <setter property="fontsize" value="{themeresource controlcontentthemefontsize}"/>         <setter property="usesystemfocusvisuals" value="true"/>         <setter property="template">             <setter.value>                 <controltemplate targettype="button">                     <grid x:name="rootgrid" background="{templatebinding background}">                         <visualstatemanager.visualstategroups>                             <visualstategroup x:name="commonstates">                                 <visualstate x:name="normal">                                     <storyboard>                                         <pointerupthemeanimation storyboard.targetname="rootgrid"/>                                     </storyboard>                                 </visualstate>                                 <visualstate x:name="pointerover">                                     <storyboard>                                         <objectanimationusingkeyframes storyboard.targetproperty="borderbrush" storyboard.targetname="contentpresenter">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontrolhighlightbasemediumlowbrush}"/>                                         </objectanimationusingkeyframes>                                         <objectanimationusingkeyframes storyboard.targetproperty="foreground" storyboard.targetname="contentpresenter">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontrolhighlightbasehighbrush}"/>                                         </objectanimationusingkeyframes>                                         <pointerupthemeanimation storyboard.targetname="rootgrid"/>                                     </storyboard>                                 </visualstate>                                 <visualstate x:name="pressed">                                     <storyboard>                                         <objectanimationusingkeyframes storyboard.targetproperty="background" storyboard.targetname="rootgrid">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontrolbackgroundbasemediumlowbrush}"/>                                         </objectanimationusingkeyframes>                                         <objectanimationusingkeyframes storyboard.targetproperty="borderbrush" storyboard.targetname="contentpresenter">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontrolhighlighttransparentbrush}"/>                                         </objectanimationusingkeyframes>                                          <!--here change of foreground when pressed-->                                         <objectanimationusingkeyframes storyboard.targetproperty="foreground" storyboard.targetname="contentpresenter">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontrolhighlightbasehighbrush}"/>                                         </objectanimationusingkeyframes>                                          <pointerdownthemeanimation storyboard.targetname="rootgrid"/>                                     </storyboard>                                 </visualstate>                                 <visualstate x:name="disabled">                                     <storyboard>                                         <objectanimationusingkeyframes storyboard.targetproperty="background" storyboard.targetname="rootgrid">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontrolbackgroundbaselowbrush}"/>                                         </objectanimationusingkeyframes>                                         <objectanimationusingkeyframes storyboard.targetproperty="foreground" storyboard.targetname="contentpresenter">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontroldisabledbasemediumlowbrush}"/>                                         </objectanimationusingkeyframes>                                         <objectanimationusingkeyframes storyboard.targetproperty="borderbrush" storyboard.targetname="contentpresenter">                                             <discreteobjectkeyframe keytime="0" value="{themeresource systemcontroldisabledtransparentbrush}"/>                                         </objectanimationusingkeyframes>                                     </storyboard>                                 </visualstate>                             </visualstategroup>                         </visualstatemanager.visualstategroups>                         <contentpresenter x:name="contentpresenter" automationproperties.accessibilityview="raw" borderbrush="{templatebinding borderbrush}" borderthickness="{templatebinding borderthickness}" contenttemplate="{templatebinding contenttemplate}" contenttransitions="{templatebinding contenttransitions}" content="{templatebinding content}" horizontalcontentalignment="{templatebinding horizontalcontentalignment}" padding="{templatebinding padding}" verticalcontentalignment="{templatebinding verticalcontentalignment}"/>                     </grid>                 </controltemplate>             </setter.value>         </setter>     </style> </page.resources> 

you find out button's colors changed visual state manager, depending on current state. in pressed state find this:

 <objectanimationusingkeyframes storyboard.targetproperty="foreground" storyboard.targetname="contentpresenter">      <discreteobjectkeyframe keytime="0" value="{themeresource systemcontrolhighlightbasehighbrush}"/>  </objectanimationusingkeyframes> 

this means, no matter set foreground, when pressed, visual state manager change button's color systemcontrolhighlightbasehighbrush. of course free change value in style. apply button:

<button content="button style" style="{staticresource mybuttonstyle}"/> 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -