c# - Binding Command in DataTemplate -


i made uwp app mvvm pattern(i think :) ) app have data template listbox. listbox in view.xaml:

<listbox x:name="lbmysongs" itemssource="{binding path=mysongs}" itemtemplate="{staticresource lbsongstemplate}" grid.row="1">         <listbox.itemcontainerstyle>             <style targettype="listboxitem">                 <setter property="horizontalcontentalignment" value="stretch"/>             </style>         </listbox.itemcontainerstyle> </listbox/> 

datetemplate resource:

       <datatemplate x:key="lbsongstemplate">         <grid>             <grid.columndefinitions>                 <columndefinition width="auto"/>                 <columndefinition width="auto"/>                 <columndefinition width="*"/>                 <columndefinition width="auto"/>                 <columndefinition width="auto"/>             </grid.columndefinitions>             <textblock text="{binding path=artist}" grid.column="0"/>             <textblock text=" - " grid.column="1"/>             <textblock text="{binding path=title}" grid.column="2"/>             //here bind command, it's not binding cos itemssource have no command. viewmodel have command.             <button command="{binding path=downloadcommand}" content="{binding path=query}"/>             <textblock text="{binding duration}" grid.column="5" horizontalalignment="right"/>         </grid>        </datatemplate> 

need set datacontext button. command in viewmodel. example:

class viewmodel {     ...     public relaycommand downloadcommand { get; set; }     public observablecollection<someclass> mysongs { get; set; }     ... } 

my problem: datatemplate have itemssource = viewmodel.mysongs. "downloadcommand" in viewmodel. type in datatemplate button set binding downloadcommand?

try use:

<button command="{binding path=datacontext.downloadcommand, elementname=lbmysongs}"         commandparameter="{binding}"         content="{binding path=query}"/> 

and change

public relaycommand downloadcommand { get; set; } 

to

public relaycommand<someclass> downloadcommand { get; set; } ... downloadcommand = new relaycommand<someclass>(downloadexecute); ... private voir downloadexecute(someclass item) {     ... } 

Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -