Button Bound To Command Won't Disable
-
Working on a WPF MVVM app... I have this button:
It's bound to RunCommand:
private ICommand _RunCommand;
public ICommand RunCommand
{
get
{
if (_RunCommand == null)
{
_RunCommand = new RelayCommand(runTest, runTestCanExecute);
}
return _RunCommand;
}
}Here's the methods:
private void runTest()
{
isTesting = true;var device = (from d in tempDevices where d.DeviceId == SelectedDevice.DeviceId select d).FirstOrDefault(); diag.ExecuteTest(device, SelectedTest);
}
private bool runTestCanExecute()
{
return !isTesting;
}isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?
If it's not broken, fix it until it is
-
Working on a WPF MVVM app... I have this button:
It's bound to RunCommand:
private ICommand _RunCommand;
public ICommand RunCommand
{
get
{
if (_RunCommand == null)
{
_RunCommand = new RelayCommand(runTest, runTestCanExecute);
}
return _RunCommand;
}
}Here's the methods:
private void runTest()
{
isTesting = true;var device = (from d in tempDevices where d.DeviceId == SelectedDevice.DeviceId select d).FirstOrDefault(); diag.ExecuteTest(device, SelectedTest);
}
private bool runTestCanExecute()
{
return !isTesting;
}isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?
If it's not broken, fix it until it is
Are you sure its not disabled? You are overriding the content, so it may appear to be enabled, but really be not clickable because you aren't handling the disabled state.
-
Working on a WPF MVVM app... I have this button:
It's bound to RunCommand:
private ICommand _RunCommand;
public ICommand RunCommand
{
get
{
if (_RunCommand == null)
{
_RunCommand = new RelayCommand(runTest, runTestCanExecute);
}
return _RunCommand;
}
}Here's the methods:
private void runTest()
{
isTesting = true;var device = (from d in tempDevices where d.DeviceId == SelectedDevice.DeviceId select d).FirstOrDefault(); diag.ExecuteTest(device, SelectedTest);
}
private bool runTestCanExecute()
{
return !isTesting;
}isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?
If it's not broken, fix it until it is
Try this, change you button to use a Click="" event instead of a Command.
Never underestimate the power of human stupidity RAH
-
Are you sure its not disabled? You are overriding the content, so it may appear to be enabled, but really be not clickable because you aren't handling the disabled state.
Ya, it's still responds to clicks.
If it's not broken, fix it until it is
-
Ya, it's still responds to clicks.
If it's not broken, fix it until it is
Copy & pasted your code into a test project and it disables just fine. Make sure your DataContext is set properly or it won't know where the command is. I.e. put a break point on your CanExecute handler and make sure its actually getting called.
-
Copy & pasted your code into a test project and it disables just fine. Make sure your DataContext is set properly or it won't know where the command is. I.e. put a break point on your CanExecute handler and make sure its actually getting called.
Ok, I see what's going on. isTesting is somehow reverting to false by the time the CanExecute is called. Stay tuned.....
If it's not broken, fix it until it is
-
Working on a WPF MVVM app... I have this button:
It's bound to RunCommand:
private ICommand _RunCommand;
public ICommand RunCommand
{
get
{
if (_RunCommand == null)
{
_RunCommand = new RelayCommand(runTest, runTestCanExecute);
}
return _RunCommand;
}
}Here's the methods:
private void runTest()
{
isTesting = true;var device = (from d in tempDevices where d.DeviceId == SelectedDevice.DeviceId select d).FirstOrDefault(); diag.ExecuteTest(device, SelectedTest);
}
private bool runTestCanExecute()
{
return !isTesting;
}isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?
If it's not broken, fix it until it is
-
Working on a WPF MVVM app... I have this button:
It's bound to RunCommand:
private ICommand _RunCommand;
public ICommand RunCommand
{
get
{
if (_RunCommand == null)
{
_RunCommand = new RelayCommand(runTest, runTestCanExecute);
}
return _RunCommand;
}
}Here's the methods:
private void runTest()
{
isTesting = true;var device = (from d in tempDevices where d.DeviceId == SelectedDevice.DeviceId select d).FirstOrDefault(); diag.ExecuteTest(device, SelectedTest);
}
private bool runTestCanExecute()
{
return !isTesting;
}isTesting is defaulted to False. The button does not disable. Anyone see what's wrong?
If it's not broken, fix it until it is
Whenever you change isTesting you will need to call
RunCommand.OnCanExecuteChanged()
(If it isn't, you might want to make isTesting a property and call OnExecuteChanged from the Setter)