There is a project:

EGEModel.cs

class EGEModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } private int _currentProgress; public int currentProgress { get { return _currentProgress; } set { if (_currentProgress != value) { _currentProgress = value; OnPropertyChanged("currentProgress"); } } } public void createReport() { currentProgress = 0; ... // currentProgress += 10; ... // currentProgress += 10; } } 

MainWindow.xaml

 ... <dxb:BarButtonItem x:Name="createReportView" Command="{Binding ClickCommand}" /> ... <dxb:BarEditItem Name="ProgressBar" EditValue="{Binding ege.currentProgress, Mode=TwoWay}" /> 

Command.cs

 public class Command : ICommand { public Command(Action<object> action) { ExecuteDelegate = action; } public Predicate<object> CanExecuteDelegate { get; set; } public Action<object> ExecuteDelegate { get; set; } public bool CanExecute(object parameter) { if (CanExecuteDelegate != null) { return CanExecuteDelegate(parameter); } return true; } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { if (ExecuteDelegate != null) { ExecuteDelegate(parameter); } } } 

MainWindowViewModel.cs

 class MainWindowViewModel { public EGEModel ege { get; set; } public ICommand ClickCommand { get; set; } public MainWindowViewModel() { ClickCommand = new Command(arg => ege.createReport()); } } 

After clicking on the "createReport" button, some calculations occur and the progress bar "should" increase by 10 points as calculations do, but this does not happen. The function starts up in blocking mode. After its execution, the full filled ProgressBar is already visible. How to fix it?

  • The answer is obvious: do not run a long function in blocking mode. I suppose the whole UI hangs, right? Try dragging the program window during calculations. - VladD
  • Exactly. And how can I run a long function in non-blocking mode? I need something else during its execution, the user did not touch the interface. - Adam
  • 2
    @derkode: Well, block the interface for this time, but how else? It is easiest to launch via Task.Run , at the same time it will be possible to use async / await, if that. --- To block the interface, you can either switch to the Enabled="false" state all controls you are interested in, or put a semitransparent panel in the UI on top of the entire window. - VladD
  • one
    > It is easiest to launch via Task.Run, at the same time it will be possible to use async / await, if that. And at the same time IProgress <T>, which will lead in the right direction :). - andreycha

1 answer 1

Use also external property. Follow this link, there is an example. https://stackoverflow.com/questions/3520359/how-to-implement-a-progress-bar-using-the-mvvm-pattern

  • Add more information to the answer, you can with an example, otherwise you will be replaced now - user2455111
  • Why copy someone else's text? the link all on the "obvious" English is written)))) - Daniel Smith
  • such rules for the design of answers, otherwise you could add a comment - user2455111
  • And well, I will consider in the future. - Daniel Smith