按钮始终处于禁用状态

时间:2015-02-05 15:22:04

标签: c# wpf mvvm binding mvvm-foundation

我的按钮上有一个简单的命令绑定按钮:

<Window ...>
    <Window.DataContext>
        <vm:ShellViewModel />
    </Window.DataContext> 
    ...
    <Button Command="{Binding DoSomethingCoolCommand}" Content="Execute" />

和vm:

public class ShellViewModel : ObservableObject {

    private RelayCommand _doSomethingCoolCommand;

    public ICommand DoSomethingCoolCommand {
        get {
            return _doSomethingCoolCommand ??
                (_doSomethingCoolCommand = new RelayCommand(DoSomethingCool));
        }
    }

    private void DoSomethingCool() { ... }

但是,在应用程序/视图启动时禁用该按钮,我无法启用它。我尝试将命令执行评估传递给RelayCommand,并在视图上设置IsEnabled。我错过了什么吗?


修改

RelayCommand和ObservableObject来自 mvvm foundation 项目,如标签中所述。链接:https://mvvmfoundation.codeplex.com

1 个答案:

答案 0 :(得分:0)

您是否检查过任何按钮父母是否将其状态设置为禁用状态?这也会禁用所有子控件。

旁注:

你似乎永远不会调用CommandManager.InvalidateRequerySuggested()并且它没有在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 calling CommandManager.InvalidateRequerySuggested()`中实现,这将建议重新查询所有已注册的命令(因为按钮1中的操作可能会对许多其他命令。

例如,可能有一个IsProcessing属性,当它的值被更改时,您可以调用CommandManager.InvalidateRequerySuggested()来更新所有其他命令的状态。