Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF disable command for a while it running

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
    });
}
like image 740
BJladu4 Avatar asked Dec 02 '25 08:12

BJladu4


2 Answers

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.

like image 97
Rajeev Ranjan Avatar answered Dec 04 '25 22:12

Rajeev Ranjan


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);
like image 34
Jehof Avatar answered Dec 04 '25 22:12

Jehof



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!