c# - How to bind a keydown to a button's click event UWP? -


i have little uwp calculator app, , i'm trying figure out how bind keyboard number button's click event x:bind. in case below i'm passing content "7" button routedeventargs in viewmodel. ideally, bind both keyboard , numpad number 7 button in xaml without need separate event handler. or need instead use code behind event handler binds numbers viewmodel's event handler?

<button x:name="sevenbutton"         grid.row="4"         grid.column="2"         content="7"         click="{x:bind viewmodel.onnumberbtnclick}"></button>  //viewmodel public void onnumberbtnclick(object sender, routedeventargs args) {     if ((_outputvalue) == "0" || (isoperationperformed))         _outputvalue = "";      isoperationperformed = false;     button numberbtnclick = (button)sender;      if ((string)numberbtnclick.content == ".")     {         if (!_outputvalue.contains("."))         {             outputvalue = outputvalue + numberbtnclick.content;         }     }     else     {         outputvalue = outputvalue + numberbtnclick.content;     } }  //onpropertychanged public string outputvalue {         {         return _outputvalue;     }     set     {         _outputvalue = value;         onpropertychanged("outputvalue");     } } 

thank you, mark

so, after following of suggestions of rarencool, i've come up. there still few issues work out focus on textbox, otherwise it's working.

in view added bind viewmodel keyroutedeventargs event:

        <textblock x:name="outputvaluetextblock"                    istextselectionenabled="true"                    keydown="{x:bind viewmodel.onkeyboardnumberinput, mode=oneway}"                    text="{x:bind viewmodel.outputvalue, mode=twoway}"></textblock>      

and in viewmodel created event:

  public void onkeyboardnumberinput(object sender, keyroutedeventargs e)     {         if (e.key == virtualkey.number0 || e.key == virtualkey.numberpad0 || (isoperationperformed))             _outputvalue = "";          if ((e.key > virtualkey.number0 || e.key < virtualkey.number9) || (e.key > virtualkey.numberpad0 || e.key < virtualkey.numberpad9))         {             string numberkey = e.key.tostring();              if (e.key == virtualkey.decimal)             {                 isoperationperformed = false;                  if (!_outputvalue.contains("."))                 {                     // switch if e.key decimal , _output not contains decimal.                     switch (numberkey)                     {                         case "decimal":                             numberkey = ".";                             outputvalue = outputvalue + numberkey;                             break;                     }                 }             }             else             {                 // swtich if e.key not decimal.                 switch (numberkey)                 {                     case "number0":                     case "numberpad0":                         numberkey = "0";                         outputvalue = outputvalue + numberkey;                         break;                     case "number1":                     case "numberpad1":                         numberkey = "1";                         outputvalue = outputvalue + numberkey;                         break;                     case "number2":                     case "numberpad2":                         numberkey = "2";                         outputvalue = outputvalue + numberkey;                         break;                     case "number3":                     case "numberpad3":                         numberkey = "3";                         outputvalue = outputvalue + numberkey;                         break;                     case "number4":                     case "numberpad4":                         numberkey = "4";                         outputvalue = outputvalue + numberkey;                         break;                     case "number5":                     case "numberpad5":                         numberkey = "5";                         outputvalue = outputvalue + numberkey;                         break;                     case "number6":                     case "numberpad6":                         numberkey = "6";                         outputvalue = outputvalue + numberkey;                         break;                     case "number7":                     case "numberpad7":                         numberkey = "7";                         outputvalue = outputvalue + numberkey;                         break;                     case "number8":                     case "numberpad8":                         numberkey = "8";                         outputvalue = outputvalue + numberkey;                         break;                     case "number9":                     case "numberpad9":                         numberkey = "9";                         outputvalue = outputvalue + numberkey;                         break;                 }             }          }     } 

i'd put actual handling separate function string parameter, catch button click , page keydown events , pass in content. however, if want have 1 function, can bind both click , keydown events function both parameters typed objects, use typeof determine you'll need them.


Comments