0

在我看来,根据在文本框中输入的正确值,我在它旁边Textbox有一个和一个附加到可以或不能执行(使按钮启用或禁用)的一个。Button Command

一切正常,除了用户输入正确的值并按下 TAB 后,焦点不会移动到指定的按钮,而是移动到它之后的控件,尽管该按钮已正确启用。

换句话说,在按下 TAB 以启用按钮并获得焦点后,UI 不够快。相反,该按钮已启用,但在焦点已移动到另一个控件之后。

绑定是并且我希望拥有这样的绑定,因为对于用户输入的每个字符,很多事情都碰巧UpdateSourceTrigger拥有它。TextBoxLostFocusPropertyChanged

有什么建议么?先感谢您。

4

2 回答 2

0

Seems to me you have a couple of options:

  1. Use UpdateSourceTrigger=Explicit instead of LostFocus and attach your own LostFocus handler that both updates the binding and also sets focus on your button if relevant.
  2. Attach your own LostFocus handler that checks whether the text is valid and, if so, sets focus back on the button.

In both cases, you may need to perform the set focus in a separate dispatcher message:

private void OnLostFocus(...)
{
    if (textIsValid)
    {
        this.Dispatcher.BeginInvoke((Action)() => button.Focus());
    }
}
于 2012-05-24T07:55:31.320 回答
0

最后,我找到了一个解决方案,而不会过多地破坏我使用的 MVVM 模式(指的是具有特定名称的特定控件)。

我所做的是设置UpdateSourceTriggerPropertyChanged而不是LostFocus. 问题PropertyChanged是我ValueConverter的不再运行了。因此,尽管按钮现在可以正确启用或禁用,并且准时准备好接收焦点,但我的文本框中显示的值并不正确。

我通过收听文本框的LostFocus事件解决了这个问题,并且现在正确地使用BindingExpression.UpdateTarget()了我的显示。TextBox.Text

这里是:

<TextBox
    Text="{Binding Path=SpecialText, Converter={StaticResource myConverter}, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
    LostFocus="TextBox_LostFocus">
</TextBox>


private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    (sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateTarget();
}
于 2012-05-24T09:44:21.120 回答