Title: Get status of ICommand execute in WPF in order to update text on xaml
Tags: c#;wpf
Question: I have a view model which looks like:
public sealed class MyViewModel: INotifyPropertyChanged
public bool ShowSuccess
get { return _success; }
set
{
_success = value;
PropertyChanged?.Invoke( ... );
}
public ICommand TestCommand
get
{
_test = _test ?? new MyTestCommand();
return _test;
}
and the command
public sealed class MyTestCommand : ICommand
public bool CanExecute(object parameter)
return true;
public void Execute(object parameter)
// do stuff
and in xaml
<Button Command="{Binding TestCommand}" Content="Test" /&gt;
I want to update ShowSuccess property after executing Execute from MyTestCommand.
How to achieve that ?
Thanks
PS: I'm still newbie in WPF, just learned MVVM and custom command
Comment: I would really recommend you to use the RelayCommand https://stackoverflow.com/a/36302909/2289942

Comment: You can pass `MyViewModel` as parameter to command and update `ShowSuccess` property. It also makes sense to use generic `RelayCommand`

Here is the accepted answer: You can use DelegateCommand for this. Which helps you use one Generic class for all commands instead of creating individual Command classes.
public sealed class MyViewModel : INotifyPropertyChanged
private ICommand _test;
private bool _success;
public bool ShowSuccess
get { return _success; }
set
{
_success = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ShowSuccess)));
}
public ICommand TestCommand
get
{
_test = _test ?? new DelegateCommand((arg) =&gt; ShowSuccess = true);
return _test;
}
public event PropertyChangedEventHandler PropertyChanged;
public class DelegateCommand : ICommand
private readonly Predicate<object&gt; _canExecute;
private readonly Action<object&gt; _execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<object&gt; execute)
: this(execute, null)
public DelegateCommand(Action<object&gt; execute,
Predicate<object&gt; canExecute)
_execute = execute;
_canExecute = canExecute;
public bool CanExecute(object parameter)
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
public void Execute(object parameter)
_execute(parameter);
public void RaiseCanExecuteChanged()
CanExecuteChanged?.Invoke(this, EventArgs.Empty);

Here is another answer: I strongly recomment taking a look at ReactiveUI - UI framework based around Rx - you don't have to use all of its features, like built in dependency injection or view location, but it's also very cool. It's not worth to reinvent the wheel in this case.
It has an implementation of ICommand that not only supports asynchronous work out of the box, it also allows commands to return stuff (VERY usefull) and takes care of disabling buttons when executing.
It also comes with DynamicData - a cure to all problems related to collections.
So, most basic sample would be:
TestCommand = ReactiveCommand.CreateFromTask<int&gt;(async paramater  =&gt;{
var result = await DoStuff(parameter); // ConfigureAwait(false) might be helpful in more complex scenarios
return result + 5;
TestCommand.Log(this) // there is some customization available here
.Subscribe(x =&gt; SomeVmProperty = x;); // this always runs on Dispatcher out of the box
TestCommand.ThrownExceptions.Log(this).Subscribe(ex =&gt; HandleError(ex));
this.WhenAnyValue(x =&gt; x.SearchText) // every time property changes
.Throttle(TimeSpan.FromMilliseconds(150)) // wait 150 ms after the last change
.Select(x =&gt; SearchText)
.InvokeCommand(Search); // we pass SearchText as a parameter to Search command, error handling is done by subscribing to Search.ThrownExceptions. This will also automatically disable all buttons bound to Search command
What is even more useful, I think, is being able to subscribe in the View code behind.
// LoginView.xaml.cs
ViewModel.Login.Where(x =&gt; !x.Success).Subscribe(_ =&gt;{
PasswordBox.Clear();
PasswordBox.Focus();

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:01/27/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><wpf>', 'Title': 'Get status of ICommand execute in WPF in order to update text on xaml', 'ViewCount': 418, 'id': 59928357, 'user_AboutMe': '<p>I love PHP, MySQL, ASP.NET, SqlServer and Javascript (including Jquery)</p>\n', 'user_AccountId': 978328.0, 'user_DisplayName': 'Snake Eyes', 'user_Reputation': 15768.0, 'user_Views': 2273.0, 'user_id': 998696.0}}