简单的方式抛出一个弹出消息时,按钮点击计数器是一次以上的wpfxaml

gdx19jrr  于 2023-06-27  发布在  其他
关注(0)|答案(1)|浏览(92)

我有一个要求,从上面的截图后,输入凭据和点击初始化客户端按钮连接得到建立,并再次使用不同的凭据和初始化客户端时,它是建立连接再次,我应该阻止它一旦连接建立它不应该允许再次我如何才能实现这个简单的方法
尝试使用中继命令,但它越来越复杂,不工作,我的要求不需要禁用按钮

7nbnzgx9

7nbnzgx91#

你不需要转换器。只需将ICommand绑定到Button.Command属性。然后在视图模型类中引入一个IsConnected属性来标记当前的连接状态,并在命令CanExecute delegate中检查它:

ViewModel.cs

class ViewModel : INotifyPropertyChanged
{
  // Bind to the connect button
  public ICommand ConnectCommand => new RelayCommand(ExecuteConnectCommand, CanExecuteConnectCommand);

  // Reflect the current connection state
  private bool IsConnected { get; set; }

  // Will disable the button when IsConnected returns true
  private bool CanExecuteConnectCommand(object commandParameter)
  {
    !this.IsConnected;
  }

  private void ExecuteConnectCommand(object commandParameter)
  {
    this.IsConnected = true;

    // TODO::Create connection    
  }
}

请参阅Microsoft文档:RelayCommand实现的中继命令逻辑。

相关问题