无法执行命令在列表框中不起作用

本文关键字:不起作用 列表 执行 命令 | 更新日期: 2023-09-27 18:33:28

我有一个列表框,其中包含一些"应用程序"对象。可以启动或停止"应用程序"对象。

对于列表框中的每个元素,我都有 2 个按钮,第一个用于启动应用程序,第二个用于停止应用程序。

但是,当我单击"开始"按钮时,命令"停止"的CanExecute直到我在应用程序内部单击才会重新评估,尽管"CommandManager.InvalidateRequerySuggested();"

            <ListBox Grid.Row="1"  ItemsSource="{Binding Applications}" Grid.ColumnSpan="3" BorderThickness="0" Background="#FFE8E8E8" HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Button Margin="5,0" Content = "Start" 
                            Command="{Binding StartCommand}"
                             Visibility="{Binding IsRunning, Converter={Converters:InvertedBoolToVisibilityConverter}}"/>
                    <Button Margin="5,0"  Content = "Stop" 
                            Command="{Binding StopCommand}"
                            Visibility="{Binding IsRunning, Converter={Converters:BoolToVisibilityConverter}}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

在应用程序视图模型上:

public bool IsRunning
{
  get
  {
    return this.m_IsRunning;
  }
  set
  {
    this.m_IsRunning = value;
    this.OnPropertyChanged("IsRunning");
    CommandManager.InvalidateRequerySuggested();
  }
}
public ICommand StartCommand
{
  get
  {
    if (this.m_StartCommand == null)
    {
      this.m_StartCommand = new RelayCommand(p => !this.IsRunning, p => this.Start());
    }
    return this.m_StartCommand;
  }
}
public ICommand StopCommand
{
  get
  {
    if (this.m_StopCommand == null)
    {
      this.m_StopCommand = new RelayCommand(p => this.IsRunning, p => this.Stop());
    }
    return this.m_StopCommand;
  }
}

我的中继命令类:

public class RelayCommand : ICommand
{
    #region Member Fields
    /// <summary>
    /// Contains the list of actions.
    /// </summary>
    private readonly Action<object> _execute;
    /// <summary>
    /// Contains the predicate _canExecute.
    /// </summary>
    private readonly Predicate<object> _canExecute;
    #endregion
    #region Constructors
    /// <summary>
    /// Initializes a new instance of the <see cref="RelayCommand"/> class.
    /// </summary>
    /// <param name="execute">The execute.</param>
    public RelayCommand(Action<object> execute)
      : this(null, execute)
    {
    }
    /// <summary>
    /// Initializes a new instance of the <see cref="RelayCommand"/> class.
    /// </summary>
    /// <param name="canExecute">The can execute.</param>
    /// <param name="execute">The execute.</param>
    public RelayCommand(Predicate<object> canExecute, Action<object> execute)
    {
      if (execute == null)
      {
        throw new ArgumentNullException("execute");
      }
      this._execute = execute;
      this._canExecute = canExecute;
    }
    #endregion // Constructors
    #region ICommand Members
    /// <summary>
    /// Occurs when changes occur that affect whether or not the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
      add { CommandManager.RequerySuggested += value; }
      remove { CommandManager.RequerySuggested -= value; }
    }
    /// <summary>
    /// Defines the method that determines whether the command can execute in its current state.
    /// </summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    /// <returns>
    /// true if this command can be executed; otherwise, false.
    /// </returns>
    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
      return this._canExecute == null ? true : this._canExecute(parameter);
    }
    /// <summary>
    /// Defines the method to be called when the command is invoked.
    /// </summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    public void Execute(object parameter)
    {
      this._execute(parameter);
    }
    #endregion
}

无法执行命令在列表框中不起作用

尝试直接在StartCommand处理程序中RaiseCanExecuteChanged() StopCommand

如果您自己实现了您的命令,那么您可以添加 RaiseCanExecuteChanged 到它。它将调用 CanExecuteChanged 事件

    public void RaiseCanExecuteChanged()
    {
        if (CanExecuteChanged != null)
        {
            CanExecuteChanged(this, EventArgs.Empty);
        }
    }