我有一个绑定到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,因此按钮应启用,但看起来已禁用,直到我点击某些内容,为什么会发生这种情况?
答案 0 :(得分:5)
尝试CommandManager.InvalidateRequerySuggested()
。
此方法应该在命令上调用CanExecute()
,并且应该更新按钮的IsEnabled
。
答案 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();
}
它可能不是最优雅的解决方案,但对我有用。