I have following ICommand
assignment in MainViewModel
:
MineCommand = new ActionCommand(
c => MineRulesForPolygon(SelectedPolygon, "stringParameter"),
c => SelectedPolygon != null && !SelectedPolygon.IsLoading
&& SelectedPolygon.Records != null
&& MinSupport > 0 && MinSupport <= 1
&& MinConfidence > 0 && MinConfidence <= 1
&& !IsMining);
First argument is Execute
and the second is CanExecute
method. SelectedPolygon.IsLoading
is true until the data is loaded (SelectedPolygon
is another ViewModel, which gets selected from a collection of those models):
public void LoadData(string latColumn = "latitude", string lonColumn = "longitude", Action postLoading = null)
{
if (_context != null)
{
new Thread(() =>
{
IsLoading = true;
if (_context.SelectWithinPolygon(SqlPolygon, latColumn, lonColumn, out DataTable tab))
{
Records = tab;
}
IsLoading = false;
Application.Current.Dispatcher.Invoke(DispatcherPriority.ApplicationIdle,
new Action(() => { postLoading?.Invoke(); }));
})
{
Name = "Polygon Data Loading Thread"
}.Start();
}
}
You can see me trying almost this solution (is mixed with second answer from this question). The way of calling LoadData
from MainViewModel
is:
SelectedPolygon.LoadData(postLoading: () => MineCommand.RaiseCanExecuteChanged());
having RaiseCanExecuteChanged()
as follows:
public void RaiseCanExecuteChanged()
{
CommandManager.InvalidateRequerySuggested();
}
To ensure the condition of CanExecute
is true, I placed the breakpoint at lambda () => MineCommand.RaiseCanExecuteChanged()
directly in parameters, and evaluated those values from CanExecute
; as expected, evaluation results true. However the button bound to this command remains disabled:
<Button Grid.Row="5"
HorizontalAlignment="Center"
Content="Mine"
Style="{StaticResource CommandButtonStyle}"
Command="{Binding MineCommand}" >
</Button>
How can I force it to update its IsEnabled
property according to CanExecute
change?