wpf - TemplateBinding from a Style DataTrigger In ControlTemplate -


in following xaml i'm using rectangle border template togglebutton. want borderbrush different colour reflect changing value of togglebutton.ischecked. unfortunately attempt here of using templatebinding in datatrigger doesn't work. need instead?

<stackpanel orientation="horizontal">     <stackpanel.resources>         <controltemplate x:key="buttonasswatchtemplate">             <border borderthickness="1">                 <border.style>                     <style>                         <setter property="borderbrush" value="gainsboro" />                          <style.triggers>                             <!-- templatebinding doesn't work.-->                             <datatrigger                                                                binding={templatebinding property=ischecked}                                      value="true">                                 <setter property="borderbrush" value="black" />                             </datatrigger>                         </style.triggers>                     </style>                 </border.style>                 <rectangle fill="{templatebinding property=background}"                            width="15" height="15" />             </border>         </controltemplate>     </stackpanel.resources>     <togglebutton template="{staticresource buttonasswatchtemplate}"                   horizontalalignment="center" verticalalignment="center"                   margin="2" background="red" /> </stackpanel> 

edit

when build , reload designer following error:

error 1 property 'binding' not support values of type 'templatebindingexpression'.

solution

<stackpanel orientation="horizontal">     <stackpanel.resources>         <controltemplate x:key="buttonasswatchtemplate">                 <border x:name="swatchborder" borderthickness="1">                 <rectangle fill="{templatebinding property=background}" width="15" height="15" />             </border>         <controltemplate.triggers>             <trigger property="togglebutton.ischecked" value="true">                 <setter targetname="swatchborder" property="borderbrush" value="yellow" />             </trigger>         </controltemplate.triggers>             </controltemplate>         </stackpanel.resources>     <radiobutton template="{staticresource buttonasswatchtemplate}"         groupname="cropguidescolourradiobuttongroup"         ischecked="{binding checked}" margin="2" background="red" />     <radiobutton template="{staticresource buttonasswatchtemplate}"         groupname="cropguidescolourradiobuttongroup"          ischecked="{binding checked}" margin="2" background="black" />     ... </stackpanel> 

you use trigger in controltemplate, e.g.

<stackpanel orientation="horizontal">     <stackpanel.resources>         <controltemplate x:key="buttonasswatchtemplate">             <border x:name="myborder" borderbrush="gainsboro" borderthickness="1">                 <rectangle fill="{templatebinding property=background}" width="15" height="15" />             </border>             <controltemplate.triggers>                 <trigger property="togglebutton.ischecked" value="true">                     <setter targetname="myborder" property="borderbrush" value="black" />                 </trigger>             </controltemplate.triggers>         </controltemplate>     </stackpanel.resources>     <togglebutton template="{staticresource buttonasswatchtemplate}"               horizontalalignment="center" verticalalignment="center"               margin="2" background="red" /> </stackpanel> 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -