views:

68

answers:

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?

A: 

If performance allows you can change the UpdateSourceTrigger of those TextBox elements to PropertyChanged instead of LostFocus.

olli
Probably not going to work: it's a subclass of RichTextBox that supports property-change notification on the Document property so that the ValueConverter can parse the XAML and transform it to HTML. I can't quite see doing that on every keystroke.
Robert Rossney
If your concerned about the cost of this operation, route the property through something that triggers a timer, and only after the timer evaluates (10ms? 100ms?) actually do the conversion then.
dhopton
A: 

As performance is an issue, you might find an article written by Josh Smith useful. The context is very similar to your problem. Josh solves it by triggering the binding update manually:

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

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

    textBindingExpr.UpdateSource();
olli