Individual view is not updated from Mvvm background worker

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; //set private member
    public ICommand StartProcessing //public field used by xaml binding
    {
        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

+3
source share
2 answers

As @Doug said, since you are already setting the DataContext:

 _popUpProgressBar = new PopUpProgressBar {DataContext = context};

You can change PopUpProgressBar to

 <Window x:Class="OpeningWindow_With_ProgressBar.View.PopUpProgressBar"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ViewModel="clr-namespace:OpeningWindow_With_ProgressBar.ViewModel" Title="PopUpProgressBar" Height="150" Width="300">

<Grid>

    <StackPanel>
        <Label FontWeight="Bold">Loading Something</Label>
        <ProgressBar Minimum="0" Maximum="100" Margin="0,10,0,0" Height="25px" Width="250px" Value="{Binding Path=Progresser, Mode=OneWay}"></ProgressBar>
        <TextBlock Margin="10,10,0,0" Text="Details of loading..."></TextBlock>
    </StackPanel>

</Grid>

0

PopUpProgressBarViewModels. , PopUpProgressBar.xaml, MainWindowViewModel ( 18).

XAML , PopUpProgressBar.xaml, , , MainWindowViewModel.

, , .

0

All Articles