开发者

Buttons looks disabled until I click something

I have a button bound to a ICommand

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

After some tasks is done, I made the button visible, except开发者_如何学运维 that they look disabled until I click something, why is that? The RemoveCommand looks like below

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;
    }

after uploads, the CanExecute callback returns True, so button should be enabled, but it looks disabled till I click something, why is this happening?

Video of the Problem

Buttons looks disabled until I click something


Try CommandManager.InvalidateRequerySuggested().

This method should call the CanExecute() on the commands and that should update the IsEnabled of your buttons.

See http://msdn.microsoft.com/en-us/library/system.windows.input.commandmanager.invalidaterequerysuggested.aspx for more information.


If CommandManager.InvalidateRequerySuggested() does not do the job, try forcing the focus on the control containing the buttons at the appropiate time (MouseEnter, Loaded...):

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

It might not be the most elegant solution but worked for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜