按钮看起来已禁用,直到我点击某些内容

Jie*_*eng 6 c# wpf

我有一个按钮绑定到 ICommand

<Button Content="Remove" Command="{Binding RemoveCommand}" x:Name="btnRemove" Visibility="Collapsed" />
Run Code Online (Sandbox Code Playgroud)

完成一些任务后,我将按钮显示为可见,除非它们看起来已禁用,直到我点击某些内容,为什么会这样?该RemoveCommand看上去象下面这样

public ICommand RemoveCommand
{
    get
    {
        if (_removeCommand == null)
        {
            _removeCommand = new RelayCommand(() =>
            {
                if (RemoveRequested != null)
                    RemoveRequested(this, EventArgs.Empty);
            }, () =>
            {
                // CanExecute Callback
                if (Status == WorkStatus.Processing || Status == WorkStatus.Pending)
                {
                    Debug.WriteLine("Returning False" + Status); return false;
                }
                Debug.WriteLine("Returning True"); return true; // After uploads, this returns True, in my Output Window. 
            });
        }
        return _removeCommand;
    }
Run Code Online (Sandbox Code Playgroud)

上传后,CanExecute回调返回True,因此应启用按钮,但看起来已禁用,直到我点击某些内容,为什么会发生这种情况?

问题的视频

Pie*_*kel 5

试试CommandManager.InvalidateRequerySuggested().

此方法应调用CanExecute()命令,并应更新IsEnabled按钮.

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.invalidaterequerysuggested.aspx.