I am having problems updating a separately open window execution line from the working background inside another class.
The execution of the program is as follows:
- Download MainWindow
- Click the button to do some work, and open the progress bar pop-up window (recently opened window)
- The background worker really works and reports on progress in the pop-up progress bar
- Pop-up progress bar hopefully updated.
Progress Bar The value is tied to a property that the step-by-step debugger seems to be updating by the background worker. These changes are simply not reflected in the pop-up progress bar view. However, the binding is not broken, because if I manually try to set the property value for the progress bar, it works fine.
Also, when I put the progress bar in the originally launched MainWindow view, it updates perfectly. Any suggestions?
Here is the code:
MainWindowViewModel
public class MainWindowViewModel: BaseViewModel
{
private void PerformSomeAction()
{
var popUpProgressBar = new PopUpProgressBarViewModel();
popUpProgressBar.Show(popUpProgressBar);
var worker = new BackgroundWorker { WorkerReportsProgress = true };
worker.ProgressChanged += delegate(object s, ProgressChangedEventArgs args)
{
if (args.ProgressPercentage != popUpProgressBar.Progresser)
{
Progresser = args.ProgressPercentage;
popUpProgressBar.Progresser = args.ProgressPercentage;
}
};
worker.DoWork += delegate
{
for (int i = 0; i < 101; i++)
{
worker.ReportProgress(i);
System.Threading.Thread.Sleep(10);
}
MessageBox.Show("Done");
};
worker.RunWorkerAsync();
}
private int _progresser;
public int Progresser
{
get { return _progresser; }
set
{
if (_progresser == value) return;
_progresser = value;
OnPropertyChanged("Progresser");
}
}
private RelayCommand _startProcessing;
public ICommand StartProcessing
{
get
{
return _startProcessing = MakeCommandSafely(_startProcessing, () => PerformSomeAction());
}
}
}
PopUpProgressBarViewModel
public class PopUpProgressBarViewModel : BaseViewModel
{
private PopUpProgressBar _popUpProgressBar;
public void Show(PopUpProgressBarViewModel context)
{
_popUpProgressBar = new PopUpProgressBar {DataContext = context};
_popUpProgressBar.Show();
}
private int _progresser;
public int Progresser
{
get { return _progresser; }
set
{
if (_progresser == value) return;
_progresser = value;
OnPropertyChanged("Progresser");
}
}
}
For a complete solution file (so you can see what is happening) see here
source
share