UWP Binding to AutoSuggestBox in MVVM -


i invoking querysubmitted command of autosuggestbox control in uwp. command binds icommand in view model. problem requires accept autosuggestboxquerysubmittedeventargs pure ui , it's not acceptable in mvvm.

my code looks that:

<autosuggestbox name="searchautosuggestbox"                 placeholdertext="search keywords"                 queryicon="find"                 >     <interactivity:interaction.behaviors>         <core:eventtriggerbehavior eventname="querysubmitted">             <core:invokecommandaction command="{x:bind viewmodel.searchcommand}" />         </core:eventtriggerbehavior>     </interactivity:interaction.behaviors> </autosuggestbox> 

and view model looks that:

public delegatecommand<autosuggestboxquerysubmittedeventargs> searchcommand { get; }  public mainpageviewmodel() {     searchcommand = new delegatecommand<autosuggestboxquerysubmittedeventargs>(executemethod); }  private void executemethod(autosuggestboxquerysubmittedeventargs o) {     // code here } 

ofcours autosuggestboxquerysubmittedeventargs not acceptable in view model. looking alternatives... same goes suggestionchosen...

invokecommandaction has parameter named inputconverter can use convert event args other parameter can passed viewmodel.

first create ivalueconverter class extract need event args this:-

public class autosuggestqueryparameterconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, string language)        {           // cast value whatever eventargs class expecting here           var args = (autosuggestboxquerysubmittedeventargs)value;           // return need args           return (string)args.chosensuggestion;        } } 

then use converter in xaml this:

<page.resources>      <converters:autosuggestqueryparameterconverter x:key="argsconverter" /> </page.resources>  <autosuggestbox name="searchautosuggestbox"             placeholdertext="search keywords"             queryicon="find">     <interactivity:interaction.behaviors>       <core:eventtriggerbehavior eventname="querysubmitted">         <core:invokecommandaction                command="{x:bind viewmodel.searchcommand}"               inputconverter="{staticresource argsconverter}" />       </core:eventtriggerbehavior>     </interactivity:interaction.behaviors> </autosuggestbox> 

finally in viewmodel change command accept string parameter. have following in vm:

public delegatecommand<string> searchcommand { get; }  public mainpageviewmodel() {     searchcommand = new delegatecommand<string>(executemethod); }  private void executemethod(string o) {     // code here } 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -