wpf - C# - Bind CommandParameter to the "DataContext" of a ListViewItem -


i'd able bind commandparameter of button current listviewitem. here's xaml :

<listview grid.row="1" x:name="playlists" itemssource="{binding playlists, updatesourcetrigger=propertychanged}">     <listview.itemspanel>         <itemspaneltemplate>             <wrappanel />         </itemspaneltemplate>     </listview.itemspanel>     <listview.itemtemplate>         <datatemplate>             <stackpanel horizontalalignment="center" verticalalignment="top" width="100" margin="5">                 <button x:name="btnplayplaylist" content="play" command="{binding path=playplaylistcommand}" />             </stackpanel>         </datatemplate>     </listview.itemtemplate> </listview> 

when click btnplayplaylist button, i'd able receive in viewmodel corresponding playlist. either getting it's index in list<playlist> or playlist object directly.

is way of doing ?

thanks :)

of course there is. using command, in case should define parameter in order code behind have access model in button located.

so briefly:

<button x:name="btnplayplaylist" content="play" command="{binding path=playplaylistcommand}" commandparameter="{binding}" /> 

the command parameter whole playlist (the whole datacontext of button). in code behind command_executed, access parameter so:

var playlist = e.parameter playlist; 

here assumed datatype playlist.

note: there approach without use of commands! add event handler button , specify tag on it.

<button x:name="btnplayplaylist" content="play" click="button_click" tag="{binding}" /> 

and in code behind:

var playlist = (sender button).tag playlist; 

remember cast tag , sender , parameter


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -