0

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

xvdiff
  • 2,179
  • 2
  • 24
  • 47
  • What RelayCommand version you are using here? Is this your own custom class? – Rohit Vats Feb 05 '15 at 15:24
  • @RohitVats No, it's from the MVVM foundation project. See: https://mvvmfoundation.codeplex.com/SourceControl/latest#MvvmFoundation/MvvmFoundation.Wpf/RelayCommand.cs – xvdiff Feb 05 '15 at 15:24
  • 2
    Only reason would have been if `CanExecute` delegate returns false for DoSomethingCoolCommand but you don't have provided that delegate. So, it should be true always. Is this complete code? Do you have CanExecute delegate OR do you have some default template defined for button in application? – Rohit Vats Feb 05 '15 at 15:28
  • Is the parent enabled? – Sebastian Negraszus Feb 05 '15 at 15:29
  • @RohitVats No, I don't have that delegate and I'm also not using a template. It's just a small demo app I've made to try a few things out when I noticed that the buttons are not working. :( – xvdiff Feb 05 '15 at 15:29
  • @SebastianNegraszus The parent is a stack panel with no styles defined. – xvdiff Feb 05 '15 at 15:30
  • @RohitVats I've tried setting the delegate to '() => true', but that doesn't change anything. – xvdiff Feb 05 '15 at 15:31
  • Does `new RelayCommand(c => DoSomethingCool())` make any difference? – paul Feb 05 '15 at 15:31
  • @paul You mean "() => DoSomethingCool()", right? No, it doesn't make any difference. – xvdiff Feb 05 '15 at 15:33
  • @xvdiff - In posted code nothing seems wrong. Can you upload small sample replicating your problem somewhere we can have a look? – Rohit Vats Feb 05 '15 at 15:33
  • @RohitVats Where? Throwing everything in a pastebin or is there something like plunkr for .Net applications? – xvdiff Feb 05 '15 at 15:35
  • 1
    If it's a small one, pastebin would be good Or some link to dropbox, gitHub would be good too. – Rohit Vats Feb 05 '15 at 15:37
  • CanExecute on the command is returning false, and CanExecuteChanged is never fired. First, find out why CanExecute is returning false. Next, find out how to change that. Finally, figure out how the command can fire CanExecuteChanged so that the button re-checks CanExecute. The Aristocrats. –  Feb 05 '15 at 15:39
  • The only things I can think of to cause such behavior based on just the limited code posted is either _doSomethingCoolCommand.CanExecute is false, or a global style is disabling buttons. I suspect it's the first case here. Is `_doSomethingCoolCommand` set to something anywhere else in the code? And is the code shown here the exact code that creates the command in your application? – Rachel Feb 05 '15 at 15:45
  • @Will: That was was thinking too at first, but he never sets an canexecute call back in his constructor, so its always null and should by default MVVM Light implementation return true `return _canExecute == null ? true : _canExecute();` – Tseng Feb 06 '15 at 08:08

1 Answers1

0

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 the RelayCommand´ class, just the registration of events, as seen [here][1]. 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 call CommandManager.InvalidateRequerySuggested() to update the state of all other Commands.

Tseng
  • 61,549
  • 15
  • 193
  • 205