Data binding value not updated from another topic

I have a WPF application where I have the following StepCount property in my ViewModel that implements INotifyPropertyChanged, and then I snap to the TextBox in my view.

public int StepCount { get { return _stepCount; } set { _stepCount = value; OnPropertyChanged("StepCount"); } } 

In XAML, here is what a DataBinding looks like:

 <TextBox Text="{Binding Path=StepCount}" /> 

This works fine, and if I change the value of StepCount, the value of the Textbox is updated accordingly.

However, my problem is that I have another thread that increments StepCount, in which case the TextBox value is not updated. As soon as the stream ends, the value of the "Text box" will be updated to the correct value.

I need the Textbox value to be updated every time my other thread increases StepCount. As of now, the Textbox value only shows an update after the stream has completed.

Another thread increments StepCount, but the change is not displayed in the user interface until the thread ends.

Any ideas?

UPDATE

I appreciate all the answers. This question was puzzled because the previously working code seemed to be discontinued, as was the case with these specific bindings.

When I installed the beta version of VS 2011, it installed the beta version of .NET 4.5, and when I uninstall VS 2011 Beta under the suspicion that this might cause problems, it did not remove the beta version of .NET 4.5.

I just uninstalled the .NET 4.5 framework and performed a repair for the .NET 4.0 platform. After completing these steps, my data bindings worked correctly, and now the Textbox is updated correctly when another thread increases StepCount.

So, it looks like the .NET Framework Beta Framework may be causing data binding problems.

I will continue this by introducing a problem with Microsoft.

Thank you all for your answers.

+4
source share
2 answers

As you discovered, the WPF classes that you linked to your view in the View model work in the user interface thread.

Ideally, you should change the StepCount property for your view model using the WPF manager and the Invoke command. This will open the call correctly.

See this article for more on this process.

+1
source

Tested just now and everything is working fine

 public partial class MainWindow : Window { SimpleClass simpleClass = new SimpleClass(10); public MainWindow() { InitializeComponent(); DataContext = simpleClass; } void IncrementViewModelProperty() { simpleClass.StepCount += 11; Thread.Sleep(5000); } private void Button_Click(object sender, RoutedEventArgs e) { //works the same //Thread thread = new Thread(() => IncrementViewModelProperty()); //thread.Start(); Action act = IncrementViewModelProperty; act.BeginInvoke(null, null); } } public class SimpleClass : INotifyPropertyChanged { public SimpleClass(int stepCnt) { StepCount = stepCnt; } public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } public int StepCount { get { return _stepCount; } set { _stepCount = value; NotifyPropertyChanged("StepCount"); } } private int _stepCount; } 

then in xaml:

 <StackPanel> <TextBlock Text="{Binding Path=StepCount, Mode=OneWay}" /> <Button Content="Increment value in separate thread" Click="Button_Click" /> </StackPanel> 

BUT! if you click the button quickly and often, you have exhausted the thread pool and it really will not update the property as expected.

0
source

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


All Articles