public void OneWayBinding() { var vm = new PropertyChangedViewModel(); var control = new DependencyPropertyBehaviorTestControl() { DataContext = vm }; vm.SetReadOnlyProperty("1"); var behavior = new DependencyPropertyBehavior() { PropertyName = "Property" }; BindingOperations.SetBinding(behavior, DependencyPropertyBehavior.BindingProperty, new Binding("ReadOnlyProperty") { Mode = BindingMode.OneWay }); Interaction.GetBehaviors(control).Add(behavior); Assert.AreEqual(1, control.propertyChangedCounter); Assert.AreEqual("1", control.Property); vm.SetReadOnlyProperty("2"); Assert.AreEqual(2, control.propertyChangedCounter); Assert.AreEqual("2", control.Property); }
public void MainCaseNestedPropertiesTextBoxTest() { var propertyChangedViewModel = new PropertyChangedViewModel(); var button = new Button() { DataContext = propertyChangedViewModel }; var textBox = new TextBox(); button.Content = textBox; ShowWindow(button); var behaviorSelectedText = new DependencyPropertyBehavior() { EventName = "Content.SelectionChanged", PropertyName = "Content.SelectedText" }; var behaviorSelectionStart = new DependencyPropertyBehavior() { EventName = "Content.SelectionChanged", PropertyName = "Content.SelectionStart" }; var behaviorSelectionLength = new DependencyPropertyBehavior() { EventName = "Content.SelectionChanged", PropertyName = "Content.SelectionLength" }; Interaction.GetBehaviors(button).Add(behaviorSelectedText); Interaction.GetBehaviors(button).Add(behaviorSelectionStart); Interaction.GetBehaviors(button).Add(behaviorSelectionLength); BindingOperations.SetBinding(textBox, TextBox.TextProperty, new Binding("Text") { Mode = BindingMode.TwoWay }); BindingOperations.SetBinding(behaviorSelectedText, DependencyPropertyBehavior.BindingProperty, new Binding("SelectedText") { Mode = BindingMode.TwoWay }); BindingOperations.SetBinding(behaviorSelectionStart, DependencyPropertyBehavior.BindingProperty, new Binding("SelectionStart") { Mode = BindingMode.TwoWay }); BindingOperations.SetBinding(behaviorSelectionLength, DependencyPropertyBehavior.BindingProperty, new Binding("SelectionLength") { Mode = BindingMode.TwoWay }); EnqueueShowWindow(); EnqueueCallback(() => { propertyChangedViewModel.Text = "12345678901234567890"; propertyChangedViewModel.SelectionStart = 5; propertyChangedViewModel.SelectionLength = 10; }); EnqueueWindowUpdateLayout(); EnqueueCallback(() => { Assert.AreEqual("6789012345", propertyChangedViewModel.SelectedText); }); EnqueueTestComplete(); }
public void MainCasePasswordBoxTest() { var propertyChangedViewModel = new PropertyChangedViewModel(); var passwordBox = new PasswordBox() { DataContext = propertyChangedViewModel }; ShowWindow(passwordBox); var behavior = new DependencyPropertyBehavior(); behavior.EventName = "PasswordChanged"; behavior.PropertyName = "Password"; Interaction.GetBehaviors(passwordBox).Add(behavior); BindingOperations.SetBinding(behavior, DependencyPropertyBehavior.BindingProperty, new Binding("Text") { Mode = BindingMode.TwoWay }); Assert.AreEqual(0, propertyChangedViewModel.TextChangedCounter); EnqueueShowWindow(); EnqueueCallback(() => { passwordBox.Password = "******"; }); EnqueueWindowUpdateLayout(); EnqueueCallback(() => { Assert.AreEqual(1, propertyChangedViewModel.TextChangedCounter); Assert.AreEqual("123456", propertyChangedViewModel.Text); passwordBox.Password = ""; }); EnqueueWindowUpdateLayout(); EnqueueCallback(() => { Assert.AreEqual(2, propertyChangedViewModel.TextChangedCounter); propertyChangedViewModel.Text = "654321"; }); EnqueueWindowUpdateLayout(); EnqueueCallback(() => { Assert.AreEqual("654321", passwordBox.Password); }); EnqueueTestComplete(); }
public void ConvertableTypesTest() { var propertyChangedViewModel = new PropertyChangedViewModel() { FloatProperty = 100 }; var button = new Button() { DataContext = propertyChangedViewModel }; ShowWindow(button); var behaviorSelectedText = new DependencyPropertyBehavior() { EventName = "", PropertyName = "Width" }; Interaction.GetBehaviors(button).Add(behaviorSelectedText); BindingOperations.SetBinding(behaviorSelectedText, DependencyPropertyBehavior.BindingProperty, new Binding("FloatProperty") { Mode = BindingMode.TwoWay }); Assert.AreEqual(100, button.Width); EnqueueShowWindow(); EnqueueCallback(() => { propertyChangedViewModel.FloatProperty = 101; }); EnqueueWindowUpdateLayout(); EnqueueCallback(() => { Assert.AreEqual(101, button.Width); button.Width = 102; }); EnqueueWindowUpdateLayout(); EnqueueCallback(() => { Assert.AreEqual(102, button.Width); }); EnqueueTestComplete(); }