c# - Itemselector items use different datatemplates but should share a view model -


right i'm using custom items control 3 different data templates, 3 view models, , itemtemplate selector.

everything working, looking on improving code. here 3 data templates:

<datatemplate x:key="refreshdevicesdatatemplate">     <grid margin="5,0">         <myapp:myappbutton name="refreshbutton" command="{binding command}" tooltip="refresh devices" style="{staticresource transparentstyle}">             <rectangle x:name="refreshrectangle" width="20" height="20" tooltip="{binding tooltip}" fill="{binding iconbrush}" opacity="1" margin="0,0,0,6"/>         </myapp:myappbutton>     </grid>     <datatemplate.triggers>         <datatrigger binding="{binding iswaiting}" value="true">             <setter property="opacity" value="0.5" targetname="refreshrectangle" />         </datatrigger>                     </datatemplate.triggers> </datatemplate> <datatemplate x:key="devicedatatemplate">     <grid margin="5,0">         <rectangle x:name="iconrectangle" width="20" height="20" fill="{binding iconbrush}" opacity="0.5" tooltip="{binding devicename}"/>     </grid>     <datatemplate.triggers>         <datatrigger binding="{binding isconnected}" value="true">             <setter property="opacity" value="1" targetname="iconrectangle" />         </datatrigger>     </datatemplate.triggers> </datatemplate> <datatemplate x:key="commanddatatemplate">     <grid margin="5,0">         <myapp:myappbutton style="{dynamicresource chromelessbuttonstyle}" foreground="white" content="{binding commandname, converter={staticresource lowercaseconverter}}"                command="{binding command}" commandparameter="{binding commandparameter}" tooltip="{binding tooltip}"                fontfamily="marlett"/>     </grid> </datatemplate> 

and template selector:

<models:windowcommanditemtemplateselector x:key="windowcommandtemplateselector" devicetemplate="{staticresource devicedatatemplate}" commandtemplate="{staticresource commanddatatemplate}" refreshcommandtemplate="{staticresource refreshdevicesdatatemplate}"/> 

public override datatemplate selecttemplate(object item, dependencyobject container) {          datatemplate resulttemplate = null;          if (item deviceviewmodel)         {             resulttemplate = devicetemplate;         }         else if (item windowcommandviewmodel) // need 2 view models because can't know template use 1 class.         {             resulttemplate = commandtemplate;         }         else if (item refreshcommandviewmodel) // here         {             resulttemplate = refreshcommandtemplate;         }          return resulttemplate; } 

the template selector selects template based on view model that's passed in.

finally specify itemscontrol:

<myapp:myappwindow.windowcommands>     <myapp:windowcommands itemtemplateselector="{staticresource windowcommandtemplateselector}" itemssource="{binding windowcommands}" /> </myapp:myappwindow.windowcommands> 

here's deal: have different view model each of these data templates, really, refreshbutton , commanddatatemplate buttons different templates. have different bindings, such iswaiting, can dealt in parent view model.

does there need 1-1 correspondence between view models , datatemplates if want keep adding custom templates itemscontrol?

maybe create static property , check that? there more wpf-y way this? i'm new it. thanks.

the question confusing, think trying avoid creating multiple viewmodel types. that's easy datatemplateselector can execute whatever logic within want. might inspect value of mysharedviewmodel.command , return template depending on precisely command is, example.

does there need 1-1 correspondence between view models , datatemplates if want keep adding custom templates itemscontrol?

no, because bindings properties name @ runtime. binding command match both refreshcommandtemplate.command , deviceviewmodel.command.

the time need 1:1 mapping if use datatemplate.datatype property, xaml shortcut avoids need create datatemplateselector.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -