Пример #1
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);
            }
        }