2011-03-17 13 views
0

在Focus()調用後立即調用命令時出現問題。該命令似乎沒有使用我輸入到文本框中的好值。它使用的值是在我改變數值之前(同步問題)Silverlight焦點()然後命令調用=失敗?

p.s .:我只是打電話給btnArrow.Focus();使我輸入的文本框失去焦點。

private void TextBox_KeyDown(object sender, KeyEventArgs e) 
    { if (e.Key == Key.Enter) 
     { 
      Dispatcher.BeginInvoke(() => { btnArrow.Focus(); btnArrow.Command.Execute(null); }); 
      (sender as Control).Focus(); 
     }} 

我設法使它工作的唯一方法是這樣的線程(OMG醜,我知道!)

if (e.Key == Key.Enter) 
     { 
      btnArrow.Focus(); 
      Thread.Sleep(10); 
      btnArrow.Command.Execute(null); 
      (sender as Control).Focus(); 
     } 

我的目的只是呼籲用好文本框的值的命令。

我試過KeyUp事件了...我錯過了什麼嗎?

非常感謝!

回答

1

假設你TextBox.Text結合是雙向:

private void JTextBox_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.Key == Key.Enter) 
     { 
      // option 1 - pass the value in 
      JButton.Command.Execute(JTextBox.Text); 

      // option 2 - force the binding 
      JTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource(); 
      JButton.Command.Execute(null); 
     } 
    } 

雖然我想聽聽關於與選項2的任何缺陷...