xaml - Set a property of a nested element in an WPF style -


i have several styles 1 (left, right, center) differ in corners (if any) rounded.

<style x:key="toggleradiobuttonleft"         targettype="{x:type togglebutton}"         basedon="{staticresource {x:type togglebutton}}">     <setter property="template">         <setter.value>             <controltemplate targettype="togglebutton">                 <border borderbrush="blue"                          background="{templatebinding background}"                         padding="15,0"                          borderthickness="1"                          cornerradius="10,0,0,10">  <!-- extract -->                     <contentpresenter horizontalalignment="center"                                       verticalalignment="center"/>                 </border>             </controltemplate>         </setter.value>     </setter>     <style.triggers>         <trigger property="ischecked" value="true">             <setter property="background" value="blue"/>             <setter property="foreground" value="white"/>         </trigger>         <trigger property="ischecked" value="false">             <setter property="background" value="white"/>             <setter property="foreground" value="black"/>         </trigger>     </style.triggers> </style> 

i'd able create single base style without rounded corners (ie center button style) , 2 more based on set rounded corners left , rightmost elements, can't figure out how set border radius in derived style because it's not top level element in style.

i know 3 options customizing template in way looking for

1) create usercontrol derived togglebutton, add radiusvalue dependency property there (of type cornerradius) , use in control template: cornerradius="{templatebinding radiusvalue}".

2) use dynamic resource.

after running roadblock dynamic resources (wpf dynamic resource lookup validation.errortemplate) prefer 3rd one

3) use attached dependency property

at first created attached dp of type cornerradius (with default radius = 3)

public static class attached {     public static readonly dependencyproperty radiusvalueproperty =         dependencyproperty.registerattached("radiusvalue", typeof (cornerradius), typeof (attached), new frameworkpropertymetadata(new cornerradius(3)));       public static void setradiusvalue(dependencyobject element, cornerradius value)     {         element.setvalue(radiusvalueproperty, value);     }      public static cornerradius getradiusvalue(dependencyobject element)     {         return (cornerradius)element.getvalue(radiusvalueproperty);     } } 

after modified custom togglebutton template:

<style x:key="toggleradiobutton"          targettype="{x:type togglebutton}"          basedon="{staticresource {x:type togglebutton}}">     <setter property="template">         <setter.value>             <controltemplate targettype="togglebutton">                 <border borderbrush="blue"                  background="{templatebinding background}"                 padding="15,0"                  borderthickness="1"                  cornerradius="{binding path=(local:attached.radiusvalue),                      relativesource={relativesource templatedparent}}">                      <contentpresenter horizontalalignment="center"                                 verticalalignment="center"/>                 </border>             </controltemplate>         </setter.value>     </setter>     <style.triggers>         <trigger property="ischecked" value="true">             <setter property="background" value="blue"/>             <setter property="foreground" value="white"/>         </trigger>         <trigger property="ischecked" value="false">             <setter property="background" value="white"/>             <setter property="foreground" value="black"/>         </trigger>     </style.triggers> </style> 

the change (except key)

cornerradius="{binding path=(local:attached.radiusvalue),                 relativesource={relativesource templatedparent}}"> 

and derived style left radiobutton, based on toggleradiobutton

<style x:key="toggleradiobuttonleft"          targettype="{x:type togglebutton}"          basedon="{staticresource toggleradiobutton}">     <setter property="local:attached.radiusvalue" value="10,0,0,10"/> </style> 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -