TextBox Binding doesn't work while focused (when using uwp datagrid)

Lettuce 121 Reputation points
2020-04-07T03:10:54.553+00:00

Hello,

So I am using row details with this datagrid control: https://learn.microsoft.com/en-us/windows/communitytoolkit/controls/datagrid_guidance/rowdetails

I have a text box in my row details:

<TextBox Header="Plan description" Margin="0,120,0,0" Width="620" Height="110" AcceptsReturn="True" HorizontalAlignment="Left" VerticalAlignment="Top" Text="{Binding PlanDescription, Mode=TwoWay}" />  

The problem is when the user clicks save (a button on the command bar) while the text box is in focus, the new value does not go back to the binding list. The user would have to click on some other part of the page first before clicking save. I tried setting focus on other controls pragmatically, and/or putting the thread to sleep just before it goes into save method, but this didn't work for me.

Anyone experienced this before?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2020-04-07T04:51:44.967+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Based on your words, you are having an issue that the binding source is not updated on time when the text is changed in the TextBox. It's not the best practice to set focus state for the TextBox to update the binding source. The root problem is that the binding source should be updated on time. For this issue, UWP has a special property that could solve the issue directly.

    Please try to check the Binding.UpdateSourceTrigger Property, this property determines the timing of binding source updates for two-way bindings. You could set the property to PropertyChanged, it means the binding source is updated whenever the binding target value changes. After you set it, the binding source will be updated once the text is changed instead of when the TextBox loses focus.

    You could use it like this:

      <TextBox x:Name="MyTextBox" Width="500" Height="300" TextChanged="MyTextBox_TextChanged" Text="{Binding MyText,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>  
    

    Thank you.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful