i have following button , style in wpf , need generalize binding in datatrigger section because have near 10 similar buttons in same window , each button should binded different property (selectedpositions, selectedagencies, ....). possible implement?
<button x:name="btnposition" grid.row="0" grid.column="0" horizontalalignment="left" verticalalignment="center" command="{binding positionfiltercommand}" content="{l:translate position}" style="{staticresource newbuttonstyle}" /> <style x:key="newbuttonstyle" targettype="{x:type button}"> <setter property="foreground" value="white" /> <setter property="height" value="22" /> <setter property="width" value="auto" /> <setter property="fontfamily" value="opensans" /> <setter property="fontsize" value="13" /> <setter property="cursor" value="hand" /> <setter property="margin" value="10,2,10,0" /> <setter property="template"> <setter.value> <controltemplate targettype="button"> <border cornerradius="3"> <grid x:name="gridbutton" background="#54728e"> <grid.columndefinitions> <columndefinition width="auto" /> <columndefinition width="*" /> </grid.columndefinitions> <image x:name="img" grid.column="0" width="24" height="24" source="img/tick-white.png" visibility="visible" /> <rectangle x:name="rect" grid.column="1" fill="#54728e" radiusx="3" radiusy="3" /> <contentpresenter grid.column="1" margin="5,0,5,0" horizontalalignment="stretch" verticalalignment="center" /> </grid> </border> <controltemplate.triggers> <datatrigger binding="{binding selectedpositions}" value="{x:static sys:string.empty}"> <setter targetname="rect" property="fill" value="#8bbcdf" /> <setter targetname="img" property="visibility" value="collapsed" /> <setter targetname="gridbutton" property="background" value="#8bbcdf" /> </datatrigger> </controltemplate.triggers> </controltemplate> </setter.value> </setter> </style>
could provide me example of explained?
sure,
1 - using tag
in style
have datatrigger
as:
<datatrigger binding="{binding path=tag, relativesource={relativesource self}}" value="{x:static sys:string.empty}"> ... </datatrigger>
as usage:
<button x:name="btnposition" grid.row="0" grid.column="0" horizontalalignment="left" verticalalignment="center" command="{binding positionfiltercommand}" content="{l:translate position}" tag="{binding selectedpositions}" style="{staticresource newbuttonstyle}" />
2 - using attached property:
"local:" refers xaml namespace alias of application or if use different namespaces, namespace mycustompropertycollection
declared.
code-behind:
public class mycustompropertycollection { public static readonly dependencyproperty somestringproperty = dependencyproperty.registerattached( "somestring", typeof(string), typeof(mycustompropertycollection), new frameworkpropertymetadata(string.empty)); public static void setsomestring(uielement element, string value) { element.setvalue(somestringproperty, value); } public static string getsomestring(uielement element) { return (string)element.getvalue(somestringproperty); } }
style.datatrigger
<datatrigger binding="{binding path=(local:mycustompropertycollection.somestring), relativesource={relativesource self}}" value="{x:static sys:string.empty}"> ... </datatrigger>
usage:
<button x:name="btnposition" grid.row="0" grid.column="0" horizontalalignment="left" verticalalignment="center" command="{binding positionfiltercommand}" content="{l:translate position}" local:mycustompropertycollection.somestring="{binding selectedpositions}" style="{staticresource newbuttonstyle}" />
3 - normal dependency property
custom button
class:
public class mybutton : button { public static readonly dependencyproperty somestringproperty = dependencyproperty.register( "somestring", typeof(string), typeof(mybutton), new frameworkpropertymetadata(string.empty)); public string somestring { { return (string)getvalue(somestringproperty); } set { setvalue(somestringproperty, value); } } }
style in xaml not needs datatrigger
updated style
definition too.
so switch
<style x:key="newbuttonstyle" targettype="{x:type button}">
to
<style x:key="newbuttonstyle" targettype="{x:type local:mybutton}">
style.datatrigger
<datatrigger binding="{binding path=somestring, relativesource={relativesource self}}" value="{x:static sys:string.empty}"> ... </datatrigger>
usage:
<local:mybutton x:name="btnposition" grid.row="0" grid.column="0" horizontalalignment="left" verticalalignment="center" command="{binding positionfiltercommand}" content="{l:translate position}" somestring="{binding selectedpositions}" style="{staticresource newbuttonstyle}" />
tag
approach frowned upon. "attached property" easier implement isn't clear of indicator of dependencies custom class normal dp , ap gets way overused. take pick you'd prefer.
Comments
Post a Comment