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

时间:2010-11-29 10:56:31

标签: c# wpf

我有一个绑定到ICommand

的按钮
<Button Content="Remove" Command="{Binding RemoveCommand}" x:Name="btnRemove" Visibility="Collapsed" />

完成某些任务后,我将按钮显示为可见,但他们看起来已禁用,直到我点击某些内容为止,为什么? 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;
    }

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

Video of the Problem

2 个答案:

答案 0 :(得分:5)

尝试CommandManager.InvalidateRequerySuggested()

此方法应该在命令上调用CanExecute(),并且应该更新按钮的IsEnabled

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

答案 1 :(得分:0)

如果CommandManager.InvalidateRequerySuggested()没有完成这项工作,请尝试在适当的时间强制关注包含按钮的控件(MouseEnter,Loaded ...):

//void ParentControl_MouseEnter(object sender, MouseEventArgs e)
void ParentControl_Loaded(object sender, RoutedEventArgs e)
{
    this.Focusable = true;
    this.Focus();
}

它可能不是最优雅的解决方案,但对我有用。