From 3a23b96c40ab272d30ebc8ed678e19bbe8ca72c0 Mon Sep 17 00:00:00 2001 From: celeron533 Date: Mon, 12 Feb 2024 18:31:49 +0800 Subject: [PATCH] [bugfix] button status is not updated when complete SearchCommand --- DicomGrep/ViewModels/MainViewModel.cs | 17 ++++++++++++++--- DicomGrep/ViewModels/ViewModelBase.cs | 7 +++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/DicomGrep/ViewModels/MainViewModel.cs b/DicomGrep/ViewModels/MainViewModel.cs index 4e69439..ed8444b 100644 --- a/DicomGrep/ViewModels/MainViewModel.cs +++ b/DicomGrep/ViewModels/MainViewModel.cs @@ -186,16 +186,21 @@ public ICommand SearchCommand { get { - return _searchCommand ?? (_searchCommand = new RelayCommand(_ => DoSearch(false), _ => this.MainStatus != MainStatusEnum.Working)); + return _searchCommand ?? (_searchCommand = new RelayCommand(_ => DoSearch(false), CanExecuteSearchCommand)); } } + public bool CanExecuteSearchCommand(object obj) + { + return this.MainStatus != MainStatusEnum.Working; + } + private ICommand _searchInResultsCommand; public ICommand SearchInResultsCommand { get { - return _searchInResultsCommand ?? (_searchInResultsCommand = new RelayCommand(_ => DoSearch(true), _ => this.MainStatus != MainStatusEnum.Working)); + return _searchInResultsCommand ?? (_searchInResultsCommand = new RelayCommand(_ => DoSearch(true), CanExecuteSearchCommand)); } } @@ -204,10 +209,15 @@ public ICommand CancelCommand { get { - return _cancelCommand ?? (_cancelCommand = new RelayCommand(_ => DoCancel(), _ => this.MainStatus == MainStatusEnum.Working)); + return _cancelCommand ?? (_cancelCommand = new RelayCommand(_ => DoCancel(), CanExecuteCancelCommand)); } } + public bool CanExecuteCancelCommand(object obj) + { + return this.MainStatus == MainStatusEnum.Working; + } + private ICommand _fileOperationCommand; public ICommand FileOperationCommand { @@ -370,6 +380,7 @@ private void SearchService_OnCompletDicomFile(object sender, DicomGrepCore.Servi private void SearchService_OnSearchComplete(object sender, DicomGrepCore.Services.EventArgs.OnSearchCompleteEventArgs e) { this.MainStatus = MainStatusEnum.Complete; + InvalidateRequerySuggested(); } private void DoSearch(bool searchInResults = false) diff --git a/DicomGrep/ViewModels/ViewModelBase.cs b/DicomGrep/ViewModels/ViewModelBase.cs index 06edf04..97c3907 100644 --- a/DicomGrep/ViewModels/ViewModelBase.cs +++ b/DicomGrep/ViewModels/ViewModelBase.cs @@ -3,6 +3,8 @@ using System.ComponentModel; using System.Runtime.CompilerServices; using System.Text; +using System.Windows; +using System.Windows.Input; namespace DicomGrep.ViewModels { @@ -25,5 +27,10 @@ protected virtual bool SetProperty(ref T storage, T value, [CallerMemberName] this.OnPropertyChanged(propertyName); return true; } + + protected virtual void InvalidateRequerySuggested() + { + Application.Current?.Dispatcher.BeginInvoke((Action)CommandManager.InvalidateRequerySuggested); + } } }