.net - Command bound in a datatemplate executing in all ViewModels instances -


i have problem commands , datatemplates in wpf , don't know if it's bug or normal behavior.

i have customcontrol containing listview. control used in several views (instances of usercontrol). datatemplate of listview contained in control is:

<datatemplate x:key="standardcontentdisplaydatatemplate">     <grid d:designwidth="193.333" d:designheight="128.036" margin="25">         <button style="{binding itembuttonstyle, relativesource={relativesource ancestortype={x:type control:contentdisplay}}}"              margin="0"              verticalcontentalignment="stretch"              horizontalcontentalignment="center"             command="{binding datacontext.itemtouchcommand, relativesource={relativesource ancestortype={x:type control:contentdisplay}}}"             commandparameter="{binding id}"             background="{binding hexadecimalcolor, fallbackvalue=#ffaaaa}" borderthickness="0" horizontalalignment="stretch" padding="0">             <grid>                 <grid.rowdefinitions>                     <rowdefinition height="1*"/>                     <rowdefinition height="3*"/>                 </grid.rowdefinitions>                 <textblock text="{binding name, fallbackvalue=mon item}" horizontalalignment="center" verticalalignment="center" tooltipservice.tooltip="{binding name}" fontsize="14.667" margin="0" grid.row="0"/>                 <image margin="1,0,1,1" grid.row="1" verticalalignment="stretch" source="{binding imageurl, fallbackvalue=/logo.png, targetnullvalue=/logont.png}" stretch="uniform" />             </grid>         </button>     </grid> </datatemplate> 

the thing binding command datacontext of contentdisplay (which customcontrol) say, viewmodel.

the action linked command navigation view containing same control , command in viewmodel, bound same way (it has total of 3 levels).

when click button in top level view, navigates correctly 2nd level view, displaying other items. when viewmodel instanciated , command bound items in listview, command called once again, , again , again. theory datatemplate notifies of parents (all instances) when binding command. maybe there bug somewhere in code.

is behavior normal? , if is, there way want achieve in mvvm-respectful way?

thank in advance responses

the problem right here:

 command="{binding datacontext.itemtouchcommand, relativesource={relativesource ancestortype={x:type control:contentdisplay}}}" 

your command bound contentdisplay's viewmodel. recommend taking datatemplate , making usercontrol own backing viewmodel (with itemtouchcommand on viewmodel). expose dependency property on usercontrol takes type of item being displayed. can change datatemplate hold instance of control so

<datatemplate x:key="standardcontentdisplaydatatemplate">     <control:itemdisplaycontrol item="{binding}"/> </datatemplate> 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -