How to use ICommand
interface with MVVM?
In view I add DataContext
(which is my ViewModel : INotifyPropertyChanged
) and I bind control's property - IsInterminate
of progress bar - to property of my ViewModel
. This property of my ViewModel
isn't static, so I need instance of ViewModel
.
My questions is: how I should update this ViewModel
instance (property bound to view's progress bar) in command's method (this method is of ViewModel
)?
<StatusBar Grid.Row="1">
<StatusBar.DataContext>
<viemodel:EventsViewModel x:Name="evm"/>
</StatusBar.DataContext>
<Label x:Name="lbStatusLabel" Width="70" Height="40" Content="{Binding EventsCollection.Count}"/>
<Separator />
<ProgressBar x:Name="pbProgress" Width="300" Height="40" IsIndeterminate="{Binding Pennding}"/>
</StatusBar>
class EventsViewModel : ViewModel
{
private static FWatch fw;
private static string fileName;
private static string pathToFile;
private static string pathToDirectory;
public EventsViewModel()
{
_startCommand = new RelayCommand(OpenFileCommand);
}
private ICommand _startCommand;
public ICommand StartCommand
{
get { return _startCommand; }
}
private static ObservableCollection<Event> _eventsCollection = new ObservableCollection<Event>();
public static ObservableCollection<Event> EventsCollection
{
get { return _eventsCollection; }
}
private static string _buttonContent = "Open file";
public string ButtonContent
{
get { return _buttonContent; }
set
{
_buttonContent = value;
NotifyPropertyChanged();
}
}
private bool _pending = false;
public bool Pennding
{
get { return _pending; }
set
{
_pending = value;
NotifyPropertyChanged();
}
}
private void OpenFileCommand()
{
// Here I want to update field _pennding - is it right? Or should I delegate it?
// Should I update `Pendding` property of `ViewModel` where is command's method or I should do it in behind-code of view?
}
}