c# - Is there anyway of consolidating similar data bindings and/or triggers in XAML? -
i have user control hosts other controls. way implemented via data templates define control should associated specific view-model. these view-models have similar properties , interaction triggers. please see xaml snippet below.
the problem approach have copy-paste data bindings if want support new view-model. there way of consolidating similar data bindings and/or triggers 1 template? don't want type/copy-paste same data binding definitions each control. (yes, know, i'm lazy.)
<usercontrol.resources> <datatemplate datatype="{x:type vm:someviewmodel1}"> <textblock canvas.left="{binding left}" canvas.top="{binding top}" rendertransform="{binding transform}" height="{binding height}" width="{binding width}"> <i:interaction.triggers> <i:eventtrigger eventname="mouseenter"> <cmd:eventtocommand command="{binding mouseentercommand}"/> </i:eventtrigger> <i:eventtrigger eventname="mouseleave"> <cmd:eventtocommand command="{binding mouseleavecommand}"/> </i:eventtrigger> </i:interaction.triggers> </textblock> </datatemplate> <datatemplate datatype="{x:type vm:someviewmodel2}"> <rectangle canvas.left="{binding left}" canvas.top="{binding top}" rendertransform="{binding transform}" height="{binding height}" width="{binding width}"> <i:interaction.triggers> <i:eventtrigger eventname="mouseenter"> <cmd:eventtocommand command="{binding mouseentercommand}"/> </i:eventtrigger> <i:eventtrigger eventname="mouseleave"> <cmd:eventtocommand command="{binding mouseleavecommand}"/> </i:eventtrigger> </i:interaction.triggers> </rectangle> </datatemplate> <datatemplate datatype="{x:type vm:someviewmodel3}"> <button canvas.left="{binding left}" canvas.top="{binding top}" rendertransform="{binding transform}" height="{binding height}" width="{binding width}"> <i:interaction.triggers> <i:eventtrigger eventname="mouseenter"> <cmd:eventtocommand command="{binding mouseentercommand}"/> </i:eventtrigger> <i:eventtrigger eventname="mouseleave"> <cmd:eventtocommand command="{binding mouseleavecommand}"/> </i:eventtrigger> </i:interaction.triggers> </button> </datatemplate> <datatemplate datatype="{x:type vm:someviewmodel4}"> <!-- not want copy-paste code here... --> </datatemplate> </usercontrol.resources>
you can use common style, , put both properties , triggers (which properties) inside style, @ stackoverflow question more details.
Comments
Post a Comment