2

I've got an ItemsControl filled with dozens of items; each item is a bound text box and a couple of buttons. Because I want the user to be able to tab from text box to text box, the buttons have Focusable set to False. This works just fine. The only problem is that since the text boxes aren't losing focus, their binding isn't updating the source, so the code behind the buttons isn't working with the right values.

I can think of a way to fix this, e.g. having the Click handler for the buttons navigate through the logical tree to their associated text box and making is binding update the source explicitly. But it seems to me that there has to be a better way than that, which would probably be obvious to me if I had a better understanding of the focus model. Is there?

4

2 回答 2

1

由于性能是一个问题,您可能会发现Josh Smith 撰写的文章很有用。上下文与您的问题非常相似。Josh 通过手动触发绑定更新来解决它:

    TextBox focusedTextBox = Keyboard.FocusedElement as TextBox;
    if (focusedTextBox == null)
        return;

    BindingExpression textBindingExpr = 
      focusedTextBox.GetBindingExpression(TextBox.TextProperty);
    if (textBindingExpr == null)
        return;

    textBindingExpr.UpdateSource();
于 2010-02-20T09:13:51.423 回答
1

如果性能允许,您可以将UpdateSourceTrigger这些 TextBox 元素的 更改为PropertyChanged而不是LostFocus.

于 2010-02-19T21:01:50.607 回答