question

Lettuce-3031 avatar image
0 Votes"
Lettuce-3031 asked ·

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

Hello,

So I am using row details with this datagrid control: https://docs.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?

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RoyLi-MSFT avatar image
1 Vote"
RoyLi-MSFT answered ·

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 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks man, that worked!

0 Votes 0 · ·