當前位置: 首頁>>代碼示例>>C#>>正文


C# Button.SetCommand方法代碼示例

本文整理匯總了C#中System.Windows.Forms.Button.SetCommand方法的典型用法代碼示例。如果您正苦於以下問題:C# Button.SetCommand方法的具體用法?C# Button.SetCommand怎麽用?C# Button.SetCommand使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Forms.Button的用法示例。


在下文中一共展示了Button.SetCommand方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: AssignCommandToButton

        public void AssignCommandToButton()
        {
            CommandProvider.OnClickCount = 0;
            TestViewModel viewModel = new TestViewModel();
            using(var button = new Button()) {
                button.SetCommand(viewModel.TestCommand);
                Assert.That(viewModel.TestCommandExecuteCount, Is.EqualTo(0));

                button.PerformClick();
                Assert.That(viewModel.TestCommandExecuteCount, Is.EqualTo(1));
                Assert.That(viewModel.ExecuteParameter, Is.Null);
                Assert.That(viewModel.CanExecuteParameter, Is.Null);
                Assert.That(CommandProvider.OnClickCount, Is.EqualTo(1));

                button.SetCommand(null);
                button.PerformClick();
                Assert.That(viewModel.TestCommandExecuteCount, Is.EqualTo(1));
                Assert.That(CommandProvider.OnClickCount, Is.EqualTo(1));

                viewModel.CanExecuteTestCommand = false;
                button.SetCommand(viewModel.TestCommand);
                button.PerformClick();
                Assert.That(viewModel.TestCommandExecuteCount, Is.EqualTo(1));
                Assert.That(CommandProvider.OnClickCount, Is.EqualTo(1));

                viewModel.CanExecuteTestCommand = true;
                button.SetCommandParameter("param");
                button.PerformClick();
                Assert.That(viewModel.TestCommandExecuteCount, Is.EqualTo(2));
                Assert.That(viewModel.ExecuteParameter, Is.EqualTo("param"));
                Assert.That(viewModel.CanExecuteParameter, Is.EqualTo("param"));
                Assert.That(CommandProvider.OnClickCount, Is.EqualTo(2));
            }
        }
開發者ID:VitalyTVA,項目名稱:WinMVVM,代碼行數:34,代碼來源:CommandProviderTests.cs

示例2: UpdateIsEnabledOnCanExecuteChanged

 public void UpdateIsEnabledOnCanExecuteChanged()
 {
     bool canExecute = true;
     bool executed = false;
     var command = new DelegateCommand<string>(o => executed = true, o => canExecute);
     using(var button = new Button()) {
         button.SetCommand(command);
         Assert.That(button.Enabled, Is.True);
         canExecute = false;
         Assert.That(button.Enabled, Is.True);
         button.PerformClick();
         Assert.That(executed, Is.False);
         Assert.That(button.Enabled, Is.True);
         TestUtils.GarbageCollect();
         command.RaiseCanExecuteChanged();
         Assert.That(button.Enabled, Is.False);
         Assert.That(command.CanExecuteChangedSubscribeCount, Is.EqualTo(1));
         WeakReference handlerRef = GetHandlerRef(button);
         button.SetCommand(null);
         Assert.That(command.CanExecuteChangedSubscribeCount, Is.EqualTo(0));
         TestUtils.GarbageCollect();
         Assert.That(handlerRef.IsAlive, Is.False);
     }
 }
開發者ID:VitalyTVA,項目名稱:WinMVVM,代碼行數:24,代碼來源:CommandProviderTests.cs

示例3: CreateButtonAndAssignCommand

 WeakReference CreateButtonAndAssignCommand(ICommand command)
 {
     var button = new Button();
     button.SetCommand(command);
     return new WeakReference(button);
 }
開發者ID:VitalyTVA,項目名稱:WinMVVM,代碼行數:6,代碼來源:CommandProviderTests.cs

示例4: UpdateIsEnabledOnSetParameter

 public void UpdateIsEnabledOnSetParameter()
 {
     var command = new DelegateCommand<string>(o => { }, o => string.IsNullOrEmpty(o));
     using(var button = new Button()) {
         button.SetCommand(command);
         Assert.That(button.Enabled, Is.True);
         button.SetCommandParameter("x");
         Assert.That(button.Enabled, Is.False);
         button.SetCommandParameter(null);
         Assert.That(button.Enabled, Is.True);
         button.SetCommandParameter("x");
         Assert.That(button.Enabled, Is.False);
         button.SetCommand(null);
         Assert.That(button.Enabled, Is.True);
         button.SetCommand(command);
         Assert.That(button.Enabled, Is.False);
     }
 }
開發者ID:VitalyTVA,項目名稱:WinMVVM,代碼行數:18,代碼來源:CommandProviderTests.cs


注:本文中的System.Windows.Forms.Button.SetCommand方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。