コード例 #1
0
        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));
            }
        }
コード例 #2
0
 public void AssignCommandToLabel()
 {
     TestViewModel viewModel = new TestViewModel();
     using(var label = new Label()) {
         label.SetCommand(viewModel.TestCommand);
     }
 }
コード例 #3
0
 public void CollectButtonWithAssignedCommand()
 {
     TestViewModel viewModel = new TestViewModel();
     WeakReference reference = CreateButtonAndAssignCommand(viewModel.TestCommand);
     TestUtils.GarbageCollect();
     Assert.That(reference.IsAlive, Is.False);
     Assert.That(viewModel.TestCommand.CanExecuteChangedSubscribeCount, Is.EqualTo(1));
     viewModel.TestCommand.RaiseCanExecuteChanged();
     Assert.That(viewModel.TestCommand.CanExecuteChangedSubscribeCount, Is.EqualTo(0));
 }
コード例 #4
0
        public void BindAttachedProperty2()
        {
            var viewModel = new TestViewModel();
            using(var button = new Button()) {
                button.SetValue(TextProperty, "button1");
                button.SetDataContext(viewModel);
                button.SetBinding(() => new Button().Text, new Binding(() => new TestViewModel().StringProperty2));
                button.SetBinding(TextProperty, new Binding(() => new TestViewModel().StringProperty));
                Assert.That(button.GetValue(TextProperty), Is.EqualTo(null));
                Assert.That(button.Text, Is.EqualTo(string.Empty));

                viewModel.StringProperty = "test";
                Assert.That(button.GetValue(TextProperty), Is.EqualTo("test"));
                Assert.That(button.Text, Is.EqualTo(string.Empty));

                viewModel.StringProperty2 = "test2";
                Assert.That(button.GetValue(TextProperty), Is.EqualTo("test"));
                Assert.That(button.Text, Is.EqualTo("test2"));

                button.ClearBinding(TextProperty);
                Assert.That(button.GetValue(TextProperty), Is.Null);
            }
        }
コード例 #5
0
        public void SetClearBindings()
        {
            using(var form = new Form()) {
                var button = new Button();
                form.Controls.Add(button);
                var viewModel = new TestViewModel() { StringProperty = "test", StringProperty2 = "test2" };
                form.SetDataContext(viewModel);
                BindingManager manager = new BindingManager();

                manager.SetBinding(form, "Text", new Binding("StringProperty"));
                Assert.That(form.Text, Is.EqualTo("test"));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));
                Assert.That(manager.GetValueActions().Count(), Is.EqualTo(0));

                SetBindingAction action = manager.GetBindingActions().First();
                Assert.That(action.Control, Is.EqualTo(form));
                Assert.That(action.Property.Name, Is.EqualTo("Text"));
                Assert.That(action.Property, Is.InstanceOf<StandardPropertyDescriptor>());
                Assert.That(((Binding)action.Binding).Path, Is.EqualTo("StringProperty"));

                manager.SetBinding(button, "Text", new Binding("StringProperty2"));
                Assert.That(button.Text, Is.EqualTo("test2"));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));

                action = manager.GetBindingActions().ElementAt(1);
                Assert.That(action.Control, Is.EqualTo(button));
                Assert.That(action.Property.Name, Is.EqualTo("Text"));

                manager.SetBinding(form, "Text", new Binding("StringProperty2"));
                Assert.That(form.Text, Is.EqualTo("test2"));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));

                action = manager.GetBindingActions().First();
                Assert.That(action.Control, Is.EqualTo(form));
                Assert.That(action.Property.Name, Is.EqualTo("Text"));
                Assert.That(((Binding)action.Binding).Path, Is.EqualTo("StringProperty2"));

                manager.ClearBinding(form, "Text");
                Assert.That(form.Text, Is.EqualTo(string.Empty));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));

                action = manager.GetBindingActions().First();
                Assert.That(action.Control, Is.EqualTo(button));
                Assert.That(action.Property.Name, Is.EqualTo("Text"));

                manager.SetBinding(form, "Text", new Binding("StringProperty"));
                manager.SetBinding(form, "Tag", new Binding("StringProperty"));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(3));
                manager.ClearAllBindings(form);
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));

                action = manager.GetBindingActions().First();
                Assert.That(action.Control, Is.EqualTo(button));
                Assert.That(action.Property.Name, Is.EqualTo("Text"));

                manager.SetBinding(form, TextProperty, new Binding("StringProperty"));
                Assert.That(form.GetValue(TextProperty), Is.EqualTo("test"));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));

                action = manager.GetBindingActions().ElementAt(1);
                Assert.That(action.Control, Is.EqualTo(form));
                Assert.That(action.Property.Name, Is.EqualTo("Text"));
                Assert.That(action.Property, Is.InstanceOf<AttachedPropertyDescriptor>());
                Assert.That(((Binding)action.Binding).Path, Is.EqualTo("StringProperty"));

                manager.ClearBinding(form, TextProperty);
                Assert.That(form.GetValue(TextProperty), Is.EqualTo(null));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(1));
                Assert.That(manager.GetBindingActions().First().Property, Is.InstanceOf<StandardPropertyDescriptor>());

                manager.SetBinding(button, TextProperty, new Binding("StringProperty2"));
                Assert.That(button.GetValue(TextProperty), Is.EqualTo("test2"));
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(2));

                manager.ClearAllBindings(button);
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
            }
        }
コード例 #6
0
        public void SetClearValue()
        {
            using(var form = new Form()) {
                var button = new Button();
                form.Controls.Add(button);
                var viewModel = new TestViewModel() { StringProperty = "test", StringProperty2 = "test2" };
                form.SetDataContext(viewModel);
                BindingManager manager = new BindingManager();

                manager.SetBinding(button, DataContextProvider.DataContextProperty, new Binding(() => viewModel.StringProperty));
                manager.SetValue(button, DataContextProvider.DataContextProperty, "value");
                Assert.That(button.GetDataContext(), Is.EqualTo("value"));

                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
                Assert.That(manager.GetValueActions().Count(), Is.EqualTo(1));
                SetValueAction action = manager.GetValueActions().First();
                Assert.That(action.Control, Is.EqualTo(button));
                Assert.That(((AttachedPropertyDescriptor)action.Property).Property, Is.EqualTo(DataContextProvider.DataContextProperty));
                Assert.That(action.Value, Is.EqualTo("value"));
                Assert.That(manager.FindAction(button, AttachedPropertyDescriptor.FromAttachedProperty(DataContextProvider.DataContextProperty)), Is.EqualTo(action));

                manager.SetValue(button, DataContextProvider.DataContextProperty, "value2");
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
                Assert.That(manager.GetValueActions().Count(), Is.EqualTo(1));
                Assert.That(button.GetDataContext(), Is.EqualTo("value2"));
                action = manager.GetValueActions().First();
                Assert.That(action.Control, Is.EqualTo(button));
                Assert.That(((AttachedPropertyDescriptor)action.Property).Property, Is.EqualTo(DataContextProvider.DataContextProperty));
                Assert.That(action.Value, Is.EqualTo("value2"));

                manager.ClearValue(button, DataContextProvider.DataContextProperty);
                Assert.That(manager.GetBindingActions().Count(), Is.EqualTo(0));
                Assert.That(manager.GetValueActions().Count(), Is.EqualTo(0));
                Assert.That(button.GetDataContext(), Is.Null);
            }
        }
コード例 #7
0
 WeakReference DoCollectViewModel_TwoWay_AttachedProperty(TextBox textBox)
 {
     var viewModel = new TestViewModel();
     textBox.SetDataContext(viewModel);
     textBox.SetBinding(CommandProvider.CommandParameterProperty, new Binding(() => viewModel.StringProperty, BindingMode.TwoWay));
     return new WeakReference(viewModel);
 }
コード例 #8
0
 WeakReference DoCollectTwoWayBoudnDataControl_AttachedProperty(TestViewModel viewModel)
 {
     var textBox = new TextBox();
     textBox.SetDataContext(viewModel);
     textBox.SetBinding(CommandProvider.CommandParameterProperty, new Binding(() => viewModel.StringProperty, BindingMode.TwoWay));
     return new WeakReference(textBox);
 }
コード例 #9
0
        public void BindingWithPath()
        {
            var viewModel = new TestViewModel() { StringProperty = "test", StringProperty2 = "StringProperty2" };
            using(var button = new Button()) {
                button.SetDataContext(viewModel);
                button.SetBinding(() => button.Text, new Binding("StringProperty"));

                Assert.That(button.Text, Is.EqualTo("test"));
                viewModel.StringProperty = "test2";
                Assert.That(button.Text, Is.EqualTo("test2"));

                button.ClearBinding(() => button.Text);
                Assert.That(button.Text, Is.EqualTo(string.Empty));

                button.SetBinding(() => button.Text, new Binding("StringProperty"));
                Assert.That(button.Text, Is.EqualTo("test2"));
                button.SetBinding(() => button.Text, new Binding("StringProperty2"));
                Assert.That(button.Text, Is.EqualTo("StringProperty2"));

                viewModel = new TestViewModel() { StringProperty2 = "StringProperty2_", NestedViewModel = new NestedTestViewModel() { NestedStringProperty = "NestedStringProperty" } };
                button.SetDataContext(viewModel);
                Assert.That(button.Text, Is.EqualTo("StringProperty2_"));

                button.SetBinding(() => button.Text, new Binding("NestedViewModel.NestedStringProperty"));
                Assert.That(button.Text, Is.EqualTo("NestedStringProperty"));
                viewModel.NestedViewModel.NestedStringProperty = "NestedStringProperty2";
                Assert.That(button.Text, Is.EqualTo("NestedStringProperty2"));
                viewModel.NestedViewModel = new NestedTestViewModel() { NestedStringProperty = "NestedStringProperty3" };
                Assert.That(button.Text, Is.EqualTo("NestedStringProperty3"));
                viewModel.NestedViewModel = null;
                Assert.That(button.Text, Is.EqualTo(string.Empty));
            }
        }
コード例 #10
0
 WeakReference DoBindButtonAndGetViewModelReference(Button button)
 {
     var viewModel = new TestViewModel() { StringProperty = "test" };
     button.SetDataContext(viewModel);
     button.SetBinding("Text", new Binding(() => viewModel.StringProperty));
     return new WeakReference(viewModel);
 }
コード例 #11
0
        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");
            }
        }
コード例 #12
0
        public void TwoWayBinding()
        {
            var viewModel = new TestViewModel() { };
            using(var textBox = new TextBox()) {
                textBox.SetDataContext(viewModel);
                textBox.SetBinding(() => textBox.Text, new Binding(() => viewModel.StringProperty));
                Assert.That(textBox.Text, Is.EqualTo(string.Empty));
                textBox.Text = "test";
                Assert.That(viewModel.StringProperty, Is.EqualTo(null));

                textBox.SetBinding(() => textBox.Text, new Binding(() => viewModel.StringProperty, BindingMode.TwoWay));
                Assert.That(textBox.Text, Is.EqualTo(string.Empty));
                textBox.Text = "test";
                Assert.That(viewModel.StringProperty, Is.EqualTo("test"));

            }
        }
コード例 #13
0
        public void ExtractPathFromLambdaExpression()
        {
            var viewModel = new TestViewModel() { StringProperty = "StringProperty", NestedViewModel = new NestedTestViewModel() { NestedStringProperty = "NestedStringProperty" } };
            using(var button = new Button()) {
                button.SetDataContext(viewModel);
                button.SetBinding(() => button.Text, new Binding(() => viewModel.StringProperty));
                Assert.That(button.Text, Is.EqualTo("StringProperty"));
                button.SetBinding(() => button.Text, new Binding(() => viewModel.NestedViewModel.NestedStringProperty));
                Assert.That(button.Text, Is.EqualTo("NestedStringProperty"));

                button.SetBinding(() => new Button().Text, new Binding(() => new TestViewModel().StringProperty));
                Assert.That(button.Text, Is.EqualTo("StringProperty"));
                button.SetBinding(() => new Button().Text, new Binding(() => new TestViewModel().NestedViewModel.NestedStringProperty));
                Assert.That(button.Text, Is.EqualTo("NestedStringProperty"));
            }
        }
コード例 #14
0
        public void ConvertToTargetPropertyType()
        {
            var viewModel = new TestViewModel() { IntProperty = 9, DoubleProperty = 153.2 };
            using(var button = new Button()) {
                button.SetDataContext(viewModel);
                button.SetBinding(() => button.Text, new Binding(() => viewModel.IntProperty));
                Assert.That(button.Text, Is.EqualTo("9"));
                viewModel.IntProperty = 13;
                Assert.That(button.Text, Is.EqualTo("13"));

                button.SetBinding(() => button.TabIndex, new Binding(() => viewModel.DoubleProperty));
                Assert.That(button.TabIndex, Is.EqualTo(153));
            }
        }
コード例 #15
0
 public void CollectBoundControl_TwoWay_AttachedProperty()
 {
     var viewModel = new TestViewModel();
     var reference = DoCollectTwoWayBoudnDataControl_AttachedProperty(viewModel);
     TestUtils.GarbageCollect();
     Assert.That(reference.IsAlive, Is.False);
     viewModel.StringProperty = "new Value";
 }
コード例 #16
0
        public void ClearTwoWayBinding()
        {
            var viewModel = new TestViewModel() { };
            using(var testControl = new TestControl()) {
                testControl.SetDataContext(viewModel);

                Assert.That(testControl.TestPropertyChangedSubscribeCount, Is.EqualTo(0));
                testControl.SetBinding(() => testControl.TestProperty, new Binding(() => viewModel.StringProperty, BindingMode.TwoWay));
                Assert.That(testControl.TestPropertyChangedSubscribeCount, Is.EqualTo(1));

                Assert.That(testControl.TestProperty, Is.Null);
                testControl.TestProperty = "test";
                Assert.That(viewModel.StringProperty, Is.EqualTo("test"));
                Assert.That(testControl.TestPropertyChangedSubscribeCount, Is.EqualTo(1));

                testControl.ClearBinding(() => testControl.TestProperty);
                Assert.That(viewModel.StringProperty, Is.EqualTo("test"));
                Assert.That(testControl.TestProperty, Is.Null);
                Assert.That(testControl.TestPropertyChangedSubscribeCount, Is.EqualTo(0));

                viewModel.StringProperty = "test2";
                Assert.That(testControl.TestProperty, Is.Null);
            }
        }
コード例 #17
0
 WeakReference DoCollectTwoWayBoudnDataControl(TestViewModel viewModel)
 {
     var textBox = new TextBox();
     textBox.SetDataContext(viewModel);
     textBox.SetBinding("Text", new Binding(() => viewModel.StringProperty, BindingMode.TwoWay));
     return new WeakReference(textBox);
 }
コード例 #18
0
        public void BindingToPathWithInheritedDataContext()
        {
            var viewModel = new TestViewModel();
            using(var form = new Form()) {
                var button1 = new Button() { Text = "button1" };
                form.Controls.Add(button1);

                form.SetDataContext(viewModel);
                button1.SetBinding(() => button1.Text, new Binding("StringProperty"));
                Assert.That(button1.Text, Is.EqualTo(string.Empty));

                viewModel.StringProperty = "test";
                Assert.That(button1.Text, Is.EqualTo("test"));
            }
        }