.net - Use same definition for two ControlTemplates -


i have definition of visual elements want use 2 differnt controls (button, thumb). there way 2 rid of duplicate code?

   <style x:key="horizontalsliderthumbstyle" targettype="{x:type thumb}">             <setter property="template">                 <setter.value>                     <controltemplate targettype="{x:type thumb}"> block same -->  <grid>                                 <visualstatemanager.visualstategroups>                                    ...                                 </visualstatemanager.visualstategroups>                                 <rectangle x:name="borderrect" fill="{templatebinding borderbrush}" />                                    ...       <style x:key="keyboardbuttonstyle" targettype="{x:type button}">             <setter property="template">                 <setter.value>                     <controltemplate targettype="{x:type button}"> block same -->  <grid>                                 <visualstatemanager.visualstategroups>                                     ...                                 </visualstatemanager.visualstategroups>                                 <rectangle x:name="borderrect" fill="{templatebinding borderbrush}" />                                    ... 

you this:

<controltemplate targettype="{x:type control}" x:key="sharedcontroltemplate">     <grid>         <rectangle fill="{templatebinding borderbrush}"/>         <textblock text="shared control template"/>     </grid> </controltemplate>  <style targettype="{x:type thumb}" x:key="horizontalsliderthumbstyle">     <setter property="template" value="{staticresource sharedcontroltemplate}"/> </style>  <style targettype="{x:type button}" x:key="keyboardbuttonstyle">     <setter property="template" value="{staticresource sharedcontroltemplate}"/> </style> 

both thumb , button derive base class of control (not immediately, it's 1 of ancestors). if define controltemplate it, may give both controls same template. then, in style, you'll tailor individual behavior, if that's need.

use example:

<thumb style="{staticresource horizontalsliderthumbstyle}" borderbrush="aliceblue"/> <button style="{staticresource keyboardbuttonstyle}" borderbrush="aquamarine"/> 

enter image description here

edit:

in comments you've mentioned wanted use contentpresenter in controltemplate. you'll have bind content property of contentcontrol. since not every control contentcontrol, work controls contain actual content... if doesn't have content, no errors occur. here's change:

<controltemplate targettype="{x:type control}" x:key="sharedcontroltemplate">     <grid>         <rectangle fill="{templatebinding borderbrush}"/>         <textblock text="shared control template"/>         <contentpresenter content="{templatebinding contentcontrol.content}"/>     </grid> </controltemplate> 

note added 1 line contentpresenter binds content of contentcontrol. if want add sort of content, it'll accept it:

<thumb style="{staticresource horizontalsliderthumbstyle}" borderbrush="aliceblue"/> <button style="{staticresource keyboardbuttonstyle}" borderbrush="aquamarine">     <rectangle fill="red" width="100" height="20"/> </button> 

enter image description here


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -