2

I need to disable button for a while it running. I have this code:

RelayCommand.cs This is my command class.

class RelayCommand : ICommand
{
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
        {
            throw new ArgumentNullException("execute");
        }

        _execute = execute;
        _canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }
}

MainWindowViewModel.cs This is my command for binding.

private RelayCommand _getTextCommand;
public ICommand GetTextCommand
{
    get
    {
        if (_getTextCommand == null)
        {
            _getTextCommand = new RelayCommand(
                param => this.GetText(param),
                param => true
                );
        }

        return _getTextCommand;
    }
}

MainWindow.xaml This is XAML code where i bind my command.

<Button x:Name="getTextButton" Command="{Binding GetTextCommand}" CommandParameter="{Binding ElementName=textTypeSelector, Path=SelectedIndex}"/>

This is the code i am launching in command:

public async void GetText(object o)
{
    await Task.Factory.StartNew(() =>
    {
        // code
    });
}
Xaruth
  • 4,034
  • 3
  • 19
  • 26
BJladu4
  • 263
  • 4
  • 15

4 Answers4

7

Try this: add a boolean property in view model and implement INotifyPropertyChanged in view model

    private bool isEnable = true;

    public bool IsEnable 
    {
        get { return isEnable; }
        set
        {
            isEnable = value; 
            NotifyPropertyChanged();
        } 
    }

    public async void GetText(object o)
    {
        await Task.Factory.StartNew(() =>
        {

            IsEnable = false;
        });
        IsEnable = true;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Bind it to button IsEnable property

<Button x:Name="abc"
                Command="{Binding GetTextCommand}"
                IsEnabled="{Binding IsEnable}" />

Set IsEnable whereever you want.

user3114639
  • 1,895
  • 16
  • 42
Rajeev Ranjan
  • 1,006
  • 8
  • 18
3

Add a boolean to your ViewModel to indicate that the command is executing and set the boolean in your GetText() method.

private bool _isRunning = false;

public async void GetText(object o)
{
    await Task.Factory.StartNew(() =>
    {
        _isRunning = true;
        CommandManager.InvalidateRequerySuggested(); 
        // code
        _isRunning = false;
        CommandManager.InvalidateRequerySuggested();
    });
}

public bool CanGetText(object o){
  return ! _isRunning;
}

Then change your RelayCommand to the following

_getTextCommand = new RelayCommand(this.GetText,CanGetText);
Jehof
  • 34,674
  • 10
  • 123
  • 155
  • it is good, but 1 issue. After code is done, the button do not sets in enabled state. It sets in enabled state after clicking on it. How it can be solved? – BJladu4 Mar 13 '14 at 09:33
  • @user2932802 ok, i think you need to use a CanExecute method in your ViewModel. I´ll update the code. Normally should CommandManager.InvalidateRequerySuggested() check and enable the button state – Jehof Mar 13 '14 at 09:37
  • you can shorten it with `new RelayCommand(this.GetText, CanGetText);` – default Mar 13 '14 at 09:41
  • update. if i minimize window or click in random place on it, button will be enabled – BJladu4 Mar 13 '14 at 09:56
2

The problem is that you are passing true for the CanExecute delegate. Pass it a method that will execute every time it needs evaluation, and within that method you can test to see whether the Command should be executable or otherwise.

stevethethread
  • 2,524
  • 2
  • 30
  • 29
  • One thing I would think about is looking at using RX with ICommand. I have been reliably informed that it helps eliminate some of the issues you see with buttons being disabled or otherwise based on CanExecute. See here http://stackoverflow.com/questions/1763411/reactive-extensions-rx-mvvm and here https://github.com/reactiveui/reactiveui – stevethethread Mar 14 '14 at 14:12
2

Here is an implementation that I'm using. It doesn't need an additional property in the ViewModel.

public sealed class AsyncDelegateCommand : ICommand
{
   private readonly Func<bool> _canExecute;
   private readonly Func<Task> _executeAsync;
   private Task _currentExecution;

   public AsyncDelegateCommand(Func<Task> executeAsync)
      : this(executeAsync, null)
   {
   }

   public AsyncDelegateCommand(Func<Task> executeAsync, Func<bool> canExecute)
   {
      if (executeAsync == null) throw new ArgumentNullException("executeAsync");
      _executeAsync = executeAsync;
      _canExecute = canExecute;
   }

   public event EventHandler CanExecuteChanged
   {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
   }

   public bool CanExecute(object parameter)
   {
      if (_currentExecution != null && !_currentExecution.IsCompleted)
      {
         return false;
      }

      return _canExecute == null || _canExecute();
   }

   public async void Execute(object parameter)
   {
      try
      {
         _currentExecution = _executeAsync();
         await _currentExecution;
      }
      finally
      {
         _currentExecution = null;
         CommandManager.InvalidateRequerySuggested();
      }
   }
}
cremor
  • 6,669
  • 1
  • 29
  • 72