Thursday 15 September 2011

c# - Windows Phone - custom control how propagate event to ViewModel -



c# - Windows Phone - custom control how propagate event to ViewModel -

i creating custom command menu , have listbox in control. this:

<listbox x:name="menuitemslist" grid.row="1" itemssource="{binding programlist, mode=oneway}"> <listbox.itemtemplate> <datatemplate> <stackpanel margin="10"> <textblock text="{binding title}" /> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox>

now when want grab tap event, properties class , maintain mvvm model. how can grab in mainpage.xaml.cs or in mainviewmodel?

i have code in mainpage.xaml:

<controls:bottommenu x:name="bottommenu" canvas.top="{binding menucanvastop}" width="480" height="400"> </controls:bottommenu>

i have prepare code in mainviewmodel:

public relaycommand<string> gotosectioncommand { { homecoming gotoarticlecommand ?? (gotoarticlecommand = new relaycommand<string>( navigatetosection)); } }

but don't know how can phone call it. what's best way?

edit: tried extend listbox:

<listbox x:name="menuitemslist" grid.row="1" itemssource="{binding programlist, mode=oneway}"> <listbox.itemtemplate> <datatemplate> <stackpanel margin="10"> <button content="{binding title}" command="{binding listbuttonclickcommand, source={relativesource self}}" commandparameter="{binding url}"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox>

with code-behind:

public icommand listbuttonclickcommand { { homecoming (icommand)getvalue(listbuttonclickcommandproperty); } set { setvalue(listbuttonclickcommandproperty, value); } } public static readonly dependencyproperty listbuttonclickcommandproperty = dependencyproperty.register("listbuttonclickcommand", typeof(icommand), typeof(bottommenu), new propertymetadata(null)); public bottommenu() { initializecomponent(); }

then in mainpage.xaml:

<controls:bottommenu x:name="bottommenu" canvas.top="{binding menucanvastop}" width="480" height="400" listbuttonclickcommand="{binding menuitembuttoncommand}"> </controls:bottommenu>

and in mainviewmodel:

private icommand menuitembuttoncommand; public icommand menuitembuttoncommand { { homecoming menuitembuttoncommand ?? (menuitembuttoncommand = new relaycommand( navigatetoarticlesection)); } }

for without luck. it's not working. relaycommand isn't triggered.

edit2 guess problem binding command in custom command don't know how prepare it.

you need bind usercontrol -- using "relativesource self" bind button, not want. should able utilize "elementname" binding locate user control:

<usercontrol x:name="usercontrolname" ... > ... <button content="{binding title}" command="{binding elementname=usercontrolname,path=listbuttonclickcommand}" commandparameter="{binding url}"/> ... </usercontrol>

c# xaml windows-phone-8 mvvm mvvm-light

No comments:

Post a Comment