Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top