开发者

Silverlight Focus() then Command Call = Fail?

开发者 https://www.devze.com 2023-02-17 17:26 出处:网络
Im having 开发者_运维技巧problems when calling a command right after the Focus() call. The command doesn\'t seem to use the good values I typed into the textbox. It uses the value that was there BEFOR

Im having 开发者_运维技巧problems when calling a command right after the Focus() call. The command doesn't seem to use the good values I typed into the textbox. It uses the value that was there BEFORE I changed the value (sync issue)

p.s.: I just call btnArrow.Focus(); to make the textbox i'm typing in lose its 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();
        }}

The only way I managed to make it work is with a Thread like this (OMG ugLY I know!)

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

My goal is just to have the command called with the good textbox values.

I've tried KeyUp event too... Am I missing something?

Thanks a lot !


Assuming your TextBox.Text binding is TwoWay:

    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); 
        }
    }

Although I'd like to hear about any pitfalls with option 2...

0

精彩评论

暂无评论...
验证码 换一张
取 消