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:
<Button Content="Search"
Command="{Binding StartSearchCommand}"
IsEnabled="{Binding IsNotSearching}" />
ViewModel:
private bool _isNotSearching;
public bool IsNotSearching
{
get { return _isNotSearching; }
set
{
_isNotSearching = value;
OnPropertyChanged("IsNotSearching");
}
}
private RelayCommand<object> _startSearchCommand;
public ICommand StartSearchCommand
{
get
{
if (_startSearchCommand == null)
_startSearchCommand = new RelayCommand<object>(p => ExecuteSearch());
return _startSearchCommand;
}
}
private void ExecuteSearch()
{
IsNotSearching = false;
//do some searching here
IsNotSearching = true;
}
I am not sure about looks when it was clicked and disabled - it is possible that it looks same.
But netaholic is right. If you execute logic in main UI thread it is possible that UI is frozen till your execution end (and isn't changed or changed to fast).
Try to put your logic in dispatcher. Like in next part of the code.
BTW, I took this code there.
EDIT: don't use this for your solution. This is wrong (check comments).
Use dispatcher when you need to update visual element async.
I've made an
AsyncDelegateCommand
for that reason (based on famousDelegateCommand
), it internally disable command (in UI) during executing command action:xaml:
ViewModel:
This code must work.
You could try command can execute to disable the command without adding
IsEnabled
propertyCommand handler
Xaml
The
canexecute
in the Relay Command will control theIsEnabled
property for you.