c# - Reusing DataTemplate for derived classes -
i have set of classes derive same base class. these intended tests , can run manually or automatically. want display on screen different depending on enum. read in @ run time via mef, idea 3rd party in theory write test procedure can run in different ways. wanted make way written in xaml simple possible avoid mistakes.
i have managed work using controltemplates
, datatemplate
similar this:
<controltemplate x:key="automatictemplate"> <textblock text="weeble"/> </controltemplate> <controltemplate x:key="manualtemplate"> <textblock text="wobble"/> </controltemplate> <datatemplate datatype="{x:type local:mytesthandler1}"> <control x:name="mycontrol" template="{staticresource automatictemplate}"/> <datatemplate.triggers> <datatrigger binding="{binding runmode}" value="{x:static tr:runtype.manual}"> <setter targetname="mycontrol" property="template" value="{staticresource manualtemplate}" /> </datatrigger> </datatemplate.triggers> </datatemplate>
which works have many different tests , might see need different way of displaying, increasing enum size, need create different controltemplate
datatemplate stay same each different datatype
but makes each test written need follow specific pattern work , wanted make more user-friendly implement. so, there way make more generic can similar to:
<datatemplate datatype="{x:type local:mytesthandler2}" automatic="{staticresource x}" manual="{staticresource y}"/>
i assuming needs datatemplate allow contentcontrol work within viewmodel use contentcontrol , set current test step , let wpf magic draw me, maybe there alternative ways?
i looked @ datatemplateselector
handler read in @ runtime along datatemplates
ended being xaml.
public class runmodetemplateselector : datatemplateselector { public datatemplate automatictemplate { get; set; } public datatemplate manualtemplate { get; set; } public override datatemplate selecttemplate(object item, dependencyobject container) { runtype runtype = ((testhandler)item).runmode; switch(runtype) { case runtype.automatic: return automatictemplate; case runtype.manual: return manualtemplate; case runtype.singlestep: default: return automatictemplate; } }
with xaml this:
<datatemplate x:key="automatictemplate"> <textblock text="weeble"/> </datatemplate> <datatemplate x:key="manualtemplate"> <textblock text="wobble"/> </datatemplate> <datatemplate datatype="{x:type local:mytesthandler}"> <datatemplate.resources> <tr:runmodetemplateselector manualtemplate="{staticresource manualtemplate}" automatictemplate="{staticresource automatictemplate}" x:key="mytemplateselector"/> </datatemplate.resources> <contentpresenter contenttemplateselector="{staticresource resourcekey=mytemplateselector}"/> </datatemplate>
but isn't less code , has code behind, whereas approach used trigers has none.
Comments
Post a Comment