MVVM : Binding Commands with Collection to Listbox in WPF -


i have list box in there different controls button , text box etc in item template, have collection bind listbox, works fine , want move code mvvm , , write commands in view model clicks events of buttons , how can bind collection + commands list box ??? because commands not in collection, data template list box

<datatemplate x:key="listitemtemplate">     <grid showgridlines="false">         <grid.rowdefinitions>             <rowdefinition></rowdefinition>             <rowdefinition></rowdefinition>             <rowdefinition></rowdefinition>         </grid.rowdefinitions>         <dockpanel grid.row="0" name="commentspanel" lastchildfill="false" minwidth="350">             <textblock  name="txtusername" isenabled="false" text="{binding username}"                        width="auto" dockpanel.dock="left" foreground="ghostwhite" margin="0,6,0,0"></textblock>              <textblock name="txtdate" isenabled="false" text="{binding createdt}"                       width="auto" dockpanel.dock="left" foreground="green" margin="4,6,0,0"></textblock>              <stackpanel dockpanel.dock="right" orientation="horizontal" width="{binding editpanelwidth}" x:name="editdeletepanel" visibility="{binding buttonvisibilitytext }">                 <button name="btnedit" content="edit" width="auto" dockpanel.dock="right" height="20"                   click="btnedit_click_1" margin="4,4,0,4" foreground="ghostwhite" verticalcontentalignment="top" visibility="{binding buttonvisibilitytext}"></button>                  <button name="btndelete" content="delete" width="auto" height="20" verticalcontentalignment="top" dockpanel.dock="right" visibility="{binding buttonvisibilitytext}"                 click="btndelete_click_1" margin="4"></button>              </stackpanel>              <stackpanel dockpanel.dock="right" orientation="horizontal" x:name="savecancelpanel" visibility="{binding cancelsaveenabletext}">                 <button name="btnsave" content="save" width="auto" height="20" dockpanel.dock="right"                  click="btnsave_click_1" margin="4"></button>                  <button name="btncancel" content="cancel" height="20" width="auto" dockpanel.dock="right"                  click="btncancel_click_1" margin="4"></button>              </stackpanel>         </dockpanel>         <dxe:textedit showborder="false" grid.row="1" name="txtcomment" width="auto" foreground="red"                     textwrapping="wrapwithoverflow" editvalue="{binding note}" isenabled="{binding iscommenttextenable}">          </dxe:textedit>         <dxe:textedit text=".............." grid.row="2" showborder="false" isenabled="false">          </dxe:textedit>     </grid> </datatemplate> 

and here collection + commands want bind buttons ,

public icommand cancelcommand {     { return _cancelcommand ?? (_cancelcommand = new commandhandler(cancel)); }     set { _cancelcommand = value; } }    public tlist<programnote> notescollection {     { return _notes; }     set     {         _notes = value;         raisepropertychanged("notescollection");     } } 

i know can use code bind commands button

<button command={binding cancelcommand} 

but command not present in collection , new in mvvm , kindly , may missing little thing bind commands , confused how add commands in collection , can them in view

you can bind commands data template buttons etc finding appropriate viewmodel

example

 <datatemplate x:key="listitemtemplate">     <button command="{binding datacontext.cancelcommand, relativesource={relativesource mode=findancestor, ancestortype=lixtbox}}"              commandparameter="{binding}">  </datatemplate> 

in example we'll find datacontext of lixtbox assume viewmodel bind command , pass current object command parameter perform actions on.

you'll receive item parameter in implementation of command

public void execute(object parameter) {     programnote note = parameter programnote;     //your logic here, eg cancelling download etc. } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -