The buttons look disabled until I clicked something

I have a button attached to ICommand

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

After completing some tasks, I made the button visible, except that they look disabled until I click something, why? RemoveCommand looks 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 loading, the CanExecute returns True, so the button must be turned on, but it is disabled until I clicked something, why is this happening?

Problem video

+6
c # wpf
source share
2 answers

Try CommandManager.InvalidateRequerySuggested() .

This method should call CanExecute() in the commands and should update the IsEnabled your buttons.

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

+5
source share

If CommandManager.InvalidateRequerySuggested() does not complete the task, try turning on the focus on the control containing the buttons at the appropriate time (MouseEnter, Loaded ...):

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

It may not be the most elegant solution, but it worked for me.

0
source share

All Articles