I've got a button with a simple command binding on my view:
<Window ...>
<Window.DataContext>
<vm:ShellViewModel />
</Window.DataContext>
...
<Button Command="{Binding DoSomethingCoolCommand}" Content="Execute" />
And the vm:
public class ShellViewModel : ObservableObject {
private RelayCommand _doSomethingCoolCommand;
public ICommand DoSomethingCoolCommand {
get {
return _doSomethingCoolCommand ??
(_doSomethingCoolCommand = new RelayCommand(DoSomethingCool));
}
}
private void DoSomethingCool() { ... }
However, the button is disabled at application/view startup, and I can't get it enabled. I've tried passing a command execution evaluation to the RelayCommand
and also set IsEnabled
on the view. Am I missing something?
Edit
RelayCommand and ObservableObject are from the mvvm foundation project, as stated in the tags. Link: https://mvvmfoundation.codeplex.com
Did you check if any of the Buttons parents has it's state to disabled? This would also disable all child controls.
On a side-note:
You never seem to call
CommandManager.InvalidateRequerySuggested()
and it's not implemented in theRelayCommand
class, just the registration of events, as seen here. ICommand or RelayCommand never does invalidate update it's state by default. You got to do this. Either by callingCommandManager.InvalidateRequerySuggested()
which will suggest a requery of all registered Commands (since action in button 1, can have influence on many other commands.For Example, there could be a
IsProcessing
property and when it's value is changed you could callCommandManager.InvalidateRequerySuggested()
to update the state of all other Commands.