c# - Button doesn't work in FlipView with DataTemplates - WinRT -


i'm using flipview , datatemplateselector determine @ runtime datatemplate apply show items in control.

i have 2 datatemplate's, 1 static , second can used undetermined number of items.

the problem button nothing. i've used breakpoint in savecommand when click button, doesn't break.

xaml

<page.resources>     <datatemplate x:key="firstdatatemplate">         <grid>             <textblock text="{binding content}" margin="10,0,18,18"></textblock>         </grid>     </datatemplate>      <datatemplate x:key="seconddatatemplate">         <grid>             <grid.columndefinitions>                 <columndefinition width="auto"></columndefinition>                 <columndefinition width="auto"></columndefinition>             </grid.columndefinitions>             <textbox grid.column="0" text="{binding url}"></textbox>             <button grid.column="1" name="sendbutton"                  style="{staticresource imagebuttonstyle}"                  command="{binding path=savecommand}"                 horizontalalignment="center">                 <grid>                     <image source="ms-appx:///skins/images/buton.png" stretch="none" />                     <textblock text="click me" verticalalignment="center" horizontalalignment="center" foreground="white"/>                 </grid>             </button>         </grid>     </datatemplate>     <local:mydatatemplateselector x:key="mydatatemplateselector"         firsttexttemplate="{staticresource firstdatatemplate}"     secondtexttemplate="{staticresource seconddatatemplate}">     </local:mydatatemplateselector> </page.resources>  <grid background="{themeresource applicationpagebackgroundthemebrush}">     <flipview x:name="itemgridview" itemtemplateselector="{staticresource mydatatemplateselector}"          margin="265,220,284,162">     </flipview> </grid> 

code-behind

public sealed partial class flipviewdemo : page {     public flipviewdemo()     {         this.initializecomponent();          var items = new list<baseclass>();          items.add(new firstitem         {             content="this test - content"         });          (int = 0; < 18; i++)         {             items.add(new seconditem             {                 url = "http://www.google.com/ " + i.tostring()              });         }         itemgridview.itemssource = items;     } }  public class baseclass {  }  public class firstitem : baseclass {     public string content { get; set; } }  public class seconditem : baseclass {     public string url { get; set; } }  public class mydatatemplateselector : datatemplateselector {     public datatemplate firsttexttemplate { get; set; }     public datatemplate secondtexttemplate { get; set; }      protected override datatemplate selecttemplatecore(object item, dependencyobject container)     {         if (item firstitem)             return firsttexttemplate;         if (item seconditem)             return secondtexttemplate;          return base.selecttemplatecore(item, container);     } } 

viewmodel

public class fisaobsviewmodel : viewmodelbase {     private relaycommand savecommand;      public fisaobsviewmodel()     {      }      public relaycommand savecommand     {                 {             return savecommand ?? (savecommand = new relaycommand(                 async () =>                 {                     try                     {                         messagedialog dlg = new messagedialog("message");                         await dlg.showasync();                     }                     catch (exception)                     {                         throw;                     }                 }));         }     } } 

some of links i've checked:

http://www.mutzl.com/tag/mvvm-light/

http://www.codeproject.com/articles/126249/mvvm-pattern-in-wpf-a-simple-tutorial-for-absolute

http://social.technet.microsoft.com/wiki/contents/articles/18199.event-handling-in-an-mvvm-wpf-application.aspx

the problem datatemplate referencing model, not viewmodel. command binding trying find command on model.

you need change binding source whatever element has it's datacontext set viewmodel.

{binding datacontext.savecommand, relativesource={relativesource ancestortype=page}} 

or

give page name, , use following binding:

{binding datacontext.savecommand, elementname=mypagename} 

Comments