Make TextBox Update

I have a regular TextBox wpf control associated with the string property. I need the displayed text to be updated immediately after updating the binding or .Text property. I tried

((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateSource(); ((TextBox)sender).GetBindingExpression(TextBox.TextProperty).UpdateTarget(); 

in the TextChanged event handler.

I tried UpdateSourceTrigger=Explicit by binding. I tried

 Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Input, new Action(() => { statusTextBox.Text = "newValue"; })); 

and many different combinations. But the text is displayed only after the method I updated the text box from the outputs.

XAML:

 <TextBox x:Name="txBox" Height="150" Margin="0,0,0,0" VerticalAlignment="Top" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" AcceptsReturn="True" VerticalContentAlignment="Top" Text="{Binding TextProperty}"Width="200" /> 
+4
source share
3 answers

your method, if it does a lot of work, probably supports the UI thread (), execution order). Whatever you do in this method, do it in the background thread.

 private void SomeMethod() { Task.Factory.StartNew(() => { /// do all your logic here //Update Text on the UI thread Application.Current.Dispatcher.BeginInvoke( DispatcherPriority.Input, new Action(() => { statusTextBox.Text = "newValue";})); //continue with the rest of the logic that take a long time }); 

Just make sure that if you touch any user interface elements in this method, you do this in the user interface thread, otherwise you will fail. Another, perhaps the best way to report user interface level information is RaisePropertyChanged, which you want the binding to know about, and not directly manipulate the user interface element.

+3
source

You need to use the TwoWay binding instead of changing the TextBox value, and you need to implement INotifyPropertyChanged in the associated data class.

0
source

Try the following code sequence, it worked for me -

 Textblock1.Text = "ABC"; System.Windows.Forms.Application.DoEvents(); MainWindow.InvalidateVisual(); System.Threading.Thread.Sleep(40); 
0
source

Source: https://habr.com/ru/post/1498970/


All Articles