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)); } }
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); } }
public void TwoWayBindingForAttachedProperty() { var viewModel = new TestViewModel() { }; using(var button = new Button()) { button.SetDataContext(viewModel); PropertyEntry<object> entry = CommandProvider.CommandParameterProperty.GetPropertyEntry(button); Assert.That(entry.ChangedSubscribeCount, Is.EqualTo(0)); button.SetBinding(CommandProvider.CommandParameterProperty, new Binding(() => viewModel.StringProperty, BindingMode.TwoWay)); Assert.That(entry.ChangedSubscribeCount, Is.EqualTo(1)); Assert.That(button.GetCommandParameter(), Is.Null); button.SetCommandParameter("test"); Assert.That(viewModel.StringProperty, Is.EqualTo("test")); button.ClearBinding(CommandProvider.CommandParameterProperty); Assert.That(entry.ChangedSubscribeCount, Is.EqualTo(0)); Assert.That(viewModel.StringProperty, Is.EqualTo("test")); Assert.That(button.GetCommandParameter(), Is.Null); viewModel.StringProperty = "test2"; Assert.That(button.GetCommandParameter(), Is.Null); button.SetCommandParameter("test3"); } }