C#: Disable button during search/calculation

前端 未结 4 1066
梦如初夏
梦如初夏 2021-01-16 12:58

I have a search dialog where I want to disable the search button during the search. This is the current code but the button does not get deactivated

View:

         


        
4条回答
  •  清酒与你
    2021-01-16 13:20

    You could try command can execute to disable the command without adding IsEnabled property

    private RelayCommand _startSearchCommand;
        public RelayCommand StartSearchCommand
        {
            get
            {
                return _startSearchCommand??
                       (_startSearchCommand=
                    //Added can execute command , only execute if system is not busy at the moment.
                           new RelayCommand(async () => await OnExecuteSearch(), () => !IsBusy));
            }
    
        }
    

    Command handler

    internal async Task OnExecuteSearch()
        {
            IsBusy = true;
    
            //Execute your action
            //and finally
            IsBusy = false;
    
        }
    

    Xaml

     

    The canexecute in the Relay Command will control the IsEnabled property for you.

提交回复
热议问题