xaml - Xamarin command is not working for object of Observablecollection -
i new xamarin , working on solution in facing below problem. have class model class, , class b viewmodel. model class
class : inotifypropertychanged { public string sampleprop { get; set; } public event propertychangedeventhandler propertychanged; public virtual void onpropertychanged(string propertyname) { if (propertychanged == null) return; propertychanged(this, new propertychangedeventargs(propertyname)); } }
i creating objects of class observable collection in class b.
class b { public command<string> callcommand { get; set; } public observablecollection<a> aobjectscollection { get; set; } public b() { aobjectscollection = new observablecollection<a>(); callcommand = new command<string>((string arg) => domakecall(arg)); } public void domakecall(string phnumber) { string s = phnumber; } }
assigning class b binding context main view page.
public partial class mainview : contentpage { initializecomponent (); bindingcontext = new b(); }
in main view(xaml) creating listview classb's observable collection property.
<listview x:name="messageslistview" itemssource="{binding aobjectscollection }" hasunevenrows="true" > <listview.itemtemplate> <datatemplate> <viewcell > <viewcell.view> <button x:name="btnclick" text="clickme" command="{binding callcommand}" commandparameter="sampleprop"/> </viewcell.view> </viewcell> </datatemplate> </listview.itemtemplate> </listview>
now clicking on button(btnclick) in main page not invoking command of viewmodel , execute method domakecall(string s).
could please me understand on wrong in code? , how can achieve scenario?
my command property vm class , not in model class. thing need find out how set context make work. don't want use relay command.
you're binding each viewcell model. has no reference b viewmodel (which command resides want call. fix this, need tell viewcell button command on messageslistview's bindingcontext (your b viewmodel in case). this:
bindingcontext="{binding source={x:reference messageslistview}, path=bindingcontext}"
and in full context:
<listview x:name="messageslistview" itemssource="{binding aobjectscollection }" hasunevenrows="true" > <listview.itemtemplate> <datatemplate> <viewcell x:name="viewcell"> <viewcell.view> <button x:name="btnclick" text="clickme" bindingcontext="{binding source={x:reference messageslistview}, path=bindingcontext}" command="{binding callcommand}" commandparameter="{binding source={x:reference viewcell}, path=bindingcontext}"/> </viewcell.view> </viewcell> </datatemplate> </listview.itemtemplate> </listview>
Comments
Post a Comment