c# - Access collection through MVVM Command -


in viewmodel have base card class , deck class contain observable collection of cards. here how bound in xaml

        <gridview itemssource="{binding deckcollection}" isitemclickenabled="true" grid.row="0">             <gridview.itemtemplate>                 <datatemplate>                     <button command="{binding path=??}"                             commandparameter=??                         <button.content>                             <grid>                                 <image                                      source="{binding imagepath}"                                     stretch="none"/>                             </grid>                         </button.content>                     </button>                 </datatemplate>             </gridview.itemtemplate>         </gridview> 

here classes

class deck {     private observablecollection<card> _deckcollection = new observablecollection<card>();     public observablecollection<card> deckcollection     {         { return _deckcollection; }         set { _deckcollection = value; }     }      public deck()     {         actioncommand = new mycommand();         actioncommand.canexecutefunc = obj => true;         actioncommand.executefunc = addtolist;     }       public void addtolist(object parameter)     {        var clickedcard = this;        //add card list in case not possible        //deckcollection.add(this) ?     } }  class card {     public string name { get; set; }     public int cost { get; set; }     public string imagepath { get; set; }     public mycommand actioncommand { get; set; } } 

and mycommand class

public class mycommand : icommand {      public predicate<object> canexecutefunc { get; set; }     public action<object> executefunc { get; set; }      public bool canexecute(object parameter)     {         return canexecutefunc(parameter);     }      public event eventhandler canexecutechanged;     public void execute(object parameter)     {         executefunc(parameter);     } } 

i have made suggested changes right actioncommand not visible within collection, properties belong card class can bound.

edit:i have changed xaml file following got errors

<button command="{binding relativesource={relativesource findancestor, ancestortype={x:type local:deck}, path=actioncommand}}"> 

the property 'ancestortype' not found in type 'relativesource'.

the property 'path' not found in type 'relativesource'.

the member "ancestortype" not recognized or not accessible.

the member "path" not recognized or not accessible.

unknown member 'ancestortype' on element 'relativesource'

unknown member 'path' on element 'relativesource'

please help

if want have button adds new items collection, think can solution.

in xaml:

<gridview itemssource="{binding deckcollection}" isitemclickenabled="true" grid.row="0">         <gridview.itemtemplate>             <datatemplate>                 <button>                     <button.content>                         <grid>                             <image source="{binding imagepath}"                                 stretch="none"/>                         </grid>                     </button.content>                 </button>             </datatemplate>         </gridview.itemtemplate>     </gridview> <!-- public property located in deck class --> <button command="{binding additemcommand}" content="add item"/> 

in c#:

    class deck, inotifypropertychanged /*custom implementation depends on .net version, in case .net3.5*/     {     private observablecollection<card> _deckcollection = new observablecollection<card>();     public observablecollection<card> deckcollection     {         { return _deckcollection; }         set { _deckcollection = value;                onpropertychanged(() => deckcollection); }     }      // add command     public icommand additemcommand { { return new mycommand(addtolist); } }     private void addtolist(object parameter)     {        deckcollection.add(new card());     }      public deck() { }      public event propertychangedeventhandler propertychanged;     protected void onpropertychanged<t>(expression<func<t>> expression)      {        if (propertychanged == null) return;         var body = (memberexpression)expression.body;        if (body != null) propertychanged.invoke(this, new propertychangedeventargs(body.member.name));     } } 

the main thing in situation cannot have add button inside collection.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -