简体   繁体   English

命令的CanExecute在列表框中不起作用

[英]CanExecute of command doesn't work in listbox

I've a listbox that contains some "Application" objects. 我有一个包含一些“应用程序”对象的列表框。 An "Application" object can be started or stopped. 可以启动或停止“应用程序”对象。

For each element in my Listbox I've 2 buttons, the first to start application and the second to stop the application. 对于列表框中的每个元素,我有2个按钮,第一个按钮用于启动应用程序,第二个按钮用于停止应用程序。

But, when I click on Start button, the CanExecute of command "Stop" isn't reevaluated until I click inside the application, despite the "CommandManager.InvalidateRequerySuggested();" 但是,当我单击“开始”按钮时,尽管有“ CommandManager.InvalidateRequerySuggested();”,但直到我在应用程序内部单击后,才重新评估命令“ Stop”的CanExecute。

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

On ApplicationViewModel : 在ApplicationViewModel上:

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

My RelayCommand Class : 我的RelayCommand类别:

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
}

Try RaiseCanExecuteChanged() of your StopCommand directly in your handler of StartCommand . 尝试RaiseCanExecuteChanged()您的StopCommand直接在您的处理程序StartCommand

If you have implemented the your Command yourself, then you can add RaiseCanExecuteChanged to it like. 如果您自己实现了Command,则可以向其中添加RaiseCanExecuteChanged。 It will call CanExecuteChanged event 它将调用CanExecuteChanged事件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM