c# - KeyDown Enter on the TextBox saves null value to the DataBase -


having strange behaviour. creating sort of chat, in scope of have textbox, (which holds new message) , method sends text database new message. calling sendmessage method in 2 separated ways:
1. clicking (click) on button;
2. pressing enter (keydown) on textbox.

strange behaviour: click event saves text, ok.
enter key - sends 1 of two:
or null (if in current controler session enter key first, invokes sendmessage);
or last saved message, invoked click.

here code snipets:

xaml

<border name="tbwritemessage"         grid.column="0"         grid.row="2"         background="white"         width="{binding path=actualwidth, elementname=ruler}"         borderbrush="{staticresource bluebrush}"         borderthickness="0,2,0,0">     <textbox style="{staticresource entermessagewatermark}"              text="{binding message}"              keydown="tbnewmessage_keydown"              x:name="tbnewmessage"              maxheight="21"/> </border> <button style="{staticresource button}"          name="btnenviar"         click="btnenviar_click"         grid.column="1"         grid.row="2"         verticalalignment="top"         width="100"         height="23"         content="enviar"         borderthickness="2,2,0,0"/> 

cs

private void sendmessage() {     _viewmodel.savenewmessage();     tbnewmessage.clear(); }  private void btnenviar_click(object sender, routedeventargs e) {     sendmessage(); }  private void tbnewmessage_keydown(object sender, keyeventargs e) {     if (e.key == key.enter)     {            sendmessage();     }    } 

would more grateful pointing me best approach in solving obstacle.
thank in advance!

ok, found workaround not bad.
on key enter do:
1. send focus on different object (in case button, not matter).
2. invoke sendmessage.
3. set focus on textbox.


Comments