Пример #1
0
        public void Should_Bind_To_Later_Added_Element_Path()
        {
            TextBlock  target;
            StackPanel stackPanel;

            var root = new TestRoot
            {
                Child = stackPanel = new StackPanel
                {
                    Children =
                    {
                        (target  = new TextBlock
                        {
                            Name = "target",
                        }),
                    }
                }
            };

            var binding = new Binding
            {
                ElementName = "source",
                Path        = "Text",
            };

            target.Bind(TextBox.TextProperty, binding);

            stackPanel.Children.Add(new TextBlock
            {
                Name = "source",
                Text = "foo",
            });

            Assert.Equal("foo", target.Text);
        }
        public void Should_Bind_To_Second_Ancestor()
        {
            TextBlock target;
            var       root = new TestRoot
            {
                Child = new Decorator
                {
                    Name  = "decorator1",
                    Child = new Decorator
                    {
                        Name  = "decorator2",
                        Child = target = new TextBlock(),
                    }
                },
            };

            var binding = new Binding
            {
                Path           = "Name",
                RelativeSource = new RelativeSource
                {
                    AncestorType  = typeof(Decorator),
                    AncestorLevel = 2,
                }
            };

            target.Bind(TextBox.TextProperty, binding);
            Assert.Equal("decorator1", target.Text);
        }
Пример #3
0
        public void Should_Return_FallbackValue_When_Converter_Returns_UnsetValue()
        {
            var target  = new TextBlock();
            var source  = new { A = 1, B = 2, C = 3 };
            var binding = new MultiBinding
            {
                Converter = new UnsetValueConverter(),
                Bindings  = new[]
                {
                    new Binding {
                        Path = "A"
                    },
                    new Binding {
                        Path = "B"
                    },
                    new Binding {
                        Path = "C"
                    },
                },
                FallbackValue = "fallback",
            };

            target.Bind(TextBlock.TextProperty, binding);

            Assert.Equal("fallback", target.Text);
        }
Пример #4
0
        public void OneWayToSource_Binding_Should_Not_StackOverflow_With_Null_Value()
        {
            // Issue #2912
            var target = new TextBlock {
                Text = null
            };
            var binding = new Binding
            {
                Path = "Foo",
                Mode = BindingMode.OneWayToSource,
            };

            target.Bind(TextBox.TextProperty, binding);

            var source = new Source {
                Foo = "foo"
            };

            target.DataContext = source;

            Assert.Null(source.Foo);

            // When running tests under NCrunch, NCrunch replaces the standard StackOverflowException
            // with its own, which will be caught by our code. Detect the stackoverflow anyway, by
            // making sure the target property was only set once.
            Assert.Equal(2, source.FooSetCount);
        }
Пример #5
0
        public void Should_Bind_To_Element_Path()
        {
            TextBlock target;
            var       root = new TestRoot
            {
                Child = new StackPanel
                {
                    Children =
                    {
                        new TextBlock
                        {
                            Name = "source",
                            Text = "foo",
                        },
                        (target = new TextBlock
                        {
                            Name = "target",
                        })
                    }
                }
            };

            var binding = new Binding
            {
                ElementName = "source",
                Path        = "Text",
            };

            target.Bind(TextBox.TextProperty, binding);

            Assert.Equal("foo", target.Text);
        }
        public void Should_Produce_Null_If_Ancestor_Not_Found()
        {
            TextBlock target;
            var       root = new TestRoot
            {
                Child = new Decorator
                {
                    Name  = "decorator",
                    Child = target = new TextBlock(),
                },
            };

            var binding = new Binding
            {
                Path           = "Name",
                RelativeSource = new RelativeSource
                {
                    AncestorType  = typeof(Decorator),
                    AncestorLevel = 2,
                }
            };

            target.Bind(TextBox.TextProperty, binding);
            Assert.Null(target.Text);
        }
Пример #7
0
        public void OneWayToSource_Binding_Should_React_To_DataContext_Changed()
        {
            var target = new TextBlock {
                Text = "bar"
            };
            var binding = new Binding
            {
                Path = "Foo",
                Mode = BindingMode.OneWayToSource,
            };

            target.Bind(TextBox.TextProperty, binding);

            var source = new Source {
                Foo = "foo"
            };

            target.DataContext = source;

            Assert.Equal("bar", source.Foo);
            target.Text = "baz";
            Assert.Equal("baz", source.Foo);
            source.Foo = "quz";
            Assert.Equal("baz", target.Text);
        }
Пример #8
0
        public void Should_Return_TargetNullValue_When_Value_Is_Null()
        {
            var target = new TextBlock();

            var binding = new MultiBinding
            {
                Converter = new NullValueConverter(),
                Bindings  = new[]
                {
                    new Binding {
                        Path = "A"
                    },
                    new Binding {
                        Path = "B"
                    },
                    new Binding {
                        Path = "C"
                    },
                },
                TargetNullValue = "(null)",
            };

            target.Bind(TextBlock.TextProperty, binding);

            Assert.Equal("(null)", target.Text);
        }
        public void TwoWay_Binding_Should_Not_Fail_With_Null_DataContext_Indexer()
        {
            var target = new TextBlock();

            target.DataContext = null;

            target.Bind(TextBlock.TextProperty, new Binding("[0]", BindingMode.TwoWay));
        }
Пример #10
0
        public TitleService()
        {
            var tbx = new TextBlock
            {
                MaxWidth     = 280,
                TextTrimming = TextTrimming.WordEllipsis
            };

            TitleControl = tbx;
            tbx.Bind(TextBlock.TextProperty, new Binding(nameof(Title))
            {
                Source = this
            });
            tbx.Bind(ToolTip.TipProperty, new Binding(nameof(Title))
            {
                Source = this
            });
            LeftControls = new SilentObservableCollection <IControl>();
        }
Пример #11
0
        public override IControl CreateControl()
        {
            var textBinding = new Binding(null, BindingMode.OneWay);
            var textBlock   = new TextBlock {
                Margin = new Thickness(1.0)
            };

            textBlock.Bind(TextBlock.TextProperty, textBinding);
            return(textBlock);
        }
Пример #12
0
        public void Null_Path_Should_Bind_To_DataContext()
        {
            var target = new TextBlock {
                DataContext = "foo"
            };
            var binding = new Binding();

            target.Bind(TextBlock.TextProperty, binding);

            Assert.Equal("foo", target.Text);
        }
Пример #13
0
        public void OneTime_Binding_Releases_Subscription_If_DataContext_Set_Later()
        {
            var target = new TextBlock();
            var source = new Source {
                Foo = "foo"
            };

            target.Bind(TextBlock.TextProperty, new Binding("Foo", BindingMode.OneTime));
            target.DataContext = source;

            Assert.Equal(0, source.SubscriberCount);
        }
Пример #14
0
        public void Binding_To_Types_Should_Work()
        {
            var type      = typeof(string);
            var textBlock = new TextBlock()
            {
                DataContext = type
            };

            using (textBlock.Bind(TextBlock.TextProperty, new Binding("Name")))
            {
                Assert.Equal("String", textBlock.Text);
            };
        }
Пример #15
0
        public void Source_Should_Be_Used()
        {
            var source = new Source {
                Foo = "foo"
            };
            var binding = new Binding {
                Source = source, Path = "Foo"
            };
            var target = new TextBlock();

            target.Bind(TextBlock.TextProperty, binding);

            Assert.Equal(target.Text, "foo");
        }
Пример #16
0
        public MainWindowViewModel(StackPanel panel)
        {
            _panel        = panel;
            _radioButtons = new List <IControl>();
            GetQuestions();

            //Command to submit answers
            SubmitAnswers = ReactiveCommand.Create(() =>
            {
                //In an ideal world, we'd have a separate page to input the user summoner name, and pre-load all the champions there
                //For now though, we are just going to load the champions upon submitting everything.
                var championGetter  = Program.Container.Resolve <IAPIMessages>();
                string summonerName = summonerNameInput.Text.ToLower().Replace(" ", string.Empty);
                ChampionName        = new NotifyTaskCompletion <string>(championGetter.GetChampionResult(summonerName, _radioButtons));
                Binding Champions   = new Binding("ChampionName.Result", Avalonia.Data.BindingMode.Default);
                answerBox.Bind(TextBox.TextProperty, Champions);
            });
            //Create UI For questions and add to window
            foreach (QuestionObj q in questionList.questions)
            {
                LoadUIForQuestion(q);
            }

            //Create submit button
            Button submitButton = new Button
            {
                Content = "Submit",
                Command = SubmitAnswers,
            };

            //Display box for answer
            answerBox = new TextBlock
            {
                Name = "Answer",
                Text = "",
            };

            //Input for summoner name
            summonerNameInput = new TextBox
            {
                Name = "Summoner",
                Text = "Enter Summoner Name Here",
            };

            //Add submit button and answer display to main panel
            _panel.Children.Add(summonerNameInput);
            _panel.Children.Add(submitButton);
            _panel.Children.Add(answerBox);
        }
Пример #17
0
        public void Should_Return_FallbackValue_When_Path_Not_Resolved()
        {
            var target  = new TextBlock();
            var source  = new Source();
            var binding = new Binding
            {
                Source        = source,
                Path          = "BadPath",
                FallbackValue = "foofallback",
            };

            target.Bind(TextBlock.TextProperty, binding);

            Assert.Equal("foofallback", target.Text);
        }
Пример #18
0
        public void OneTime_Binding_Releases_Subscription_If_DataContext_Set_Later()
        {
            var target = new TextBlock();
            var source = new Source {
                Foo = "foo"
            };

            target.Bind(TextBlock.TextProperty, new Binding("Foo", BindingMode.OneTime));
            target.DataContext = source;

            // Forces WeakEvent compact
            Dispatcher.UIThread.RunJobs();

            Assert.Equal(0, source.SubscriberCount);
        }
Пример #19
0
    public void TestBind(int count)
    {
        var binding = new Binding
        {
            Path = nameof(TestItem.Name),
            Mode = BindingMode.OneWay,             // copying a value to the clipboard triggers an infinite loop without this?
        };

        for (int i = 0; i < count; i++)
        {
            var textBlock = new TextBlock();
            //var testItem = new TestItem();

            textBlock.Bind(TextBlock.TextProperty, binding);
        }
    }
Пример #20
0
        public void StringFormat_Should_Be_Applied()
        {
            var textBlock = new TextBlock
            {
                DataContext = new Class1(),
            };

            var target = new Binding(nameof(Class1.Foo))
            {
                StringFormat = "Hello {0}",
            };

            textBlock.Bind(TextBlock.TextProperty, target);

            Assert.Equal("Hello foo", textBlock.Text);
        }
Пример #21
0
        public void StringFormat_Should_Be_Applied_After_Converter()
        {
            var textBlock = new TextBlock
            {
                DataContext = new Class1(),
            };

            var target = new Binding(nameof(Class1.Foo))
            {
                Converter    = StringConverters.IsNotNullOrEmpty,
                StringFormat = "Hello {0}",
            };

            textBlock.Bind(TextBlock.TextProperty, target);

            Assert.Equal("Hello True", textBlock.Text);
        }
        public void Should_Update_When_Detached_And_Attached_To_Visual_Tree_With_ComplexBindingPath()
        {
            TextBlock target;
            Decorator decorator1;
            Decorator decorator2;

            var vm = new { Foo = new  { Value = "Foo" } };

            var root1 = new TestRoot
            {
                Child = decorator1 = new Decorator
                {
                    Name  = "decorator1",
                    Child = target = new TextBlock(),
                },
                DataContext = vm
            };

            var root2 = new TestRoot
            {
                Child = decorator2 = new Decorator
                {
                    Name = "decorator2",
                },
                DataContext = vm
            };

            var binding = new Binding
            {
                Path           = "DataContext.Foo.Value",
                RelativeSource = new RelativeSource
                {
                    AncestorType = typeof(Decorator),
                }
            };

            target.Bind(TextBox.TextProperty, binding);
            Assert.Equal("Foo", target.Text);

            decorator1.Child = null;
            Assert.Null(target.Text);

            decorator2.Child = target;
            Assert.Equal("Foo", target.Text);
        }
Пример #23
0
        public void TwoWay_Binding_Should_Be_Set_Up()
        {
            var source = new Source { Foo = "foo" };
            var target = new TextBlock { DataContext = source };
            var binding = new Binding
            {
                Path = "Foo",
                Mode = BindingMode.TwoWay,
            };

            target.Bind(TextBox.TextProperty, binding);

            Assert.Equal("foo", target.Text);
            source.Foo = "bar";
            Assert.Equal("bar", target.Text);
            target.Text = "baz";
            Assert.Equal("baz", source.Foo);
        }
Пример #24
0
        public void Should_Return_TargetNullValue_When_Value_Is_Null()
        {
            var target = new TextBlock();
            var source = new Source {
                Foo = null
            };

            var binding = new Binding
            {
                Source          = source,
                Path            = "Foo",
                TargetNullValue = "(null)",
            };

            target.Bind(TextBlock.TextProperty, binding);

            Assert.Equal("(null)", target.Text);
        }
Пример #25
0
        public void Combined_OneTime_And_OneWayToSource_Bindings_Should_Release_Subscriptions()
        {
            var target1 = new TextBlock();
            var target2 = new TextBlock();
            var root    = new Panel {
                Children = { target1, target2 }
            };
            var source = new Source {
                Foo = "foo"
            };

            using (target1.Bind(TextBlock.TextProperty, new Binding("Foo", BindingMode.OneTime)))
                using (target2.Bind(TextBlock.TextProperty, new Binding("Foo", BindingMode.OneWayToSource)))
                {
                    root.DataContext = source;
                }

            Assert.Equal(0, source.SubscriberCount);
        }
Пример #26
0
        public void StringFormat_Should_Be_Applied()
        {
            var textBlock = new TextBlock
            {
                DataContext = new Class1(),
            };

            var target = new MultiBinding
            {
                StringFormat = "{0:0.0} + {1:00}",
                Bindings     =
                {
                    new Binding(nameof(Class1.Foo)),
                    new Binding(nameof(Class1.Bar)),
                }
            };

            textBlock.Bind(TextBlock.TextProperty, target);

            Assert.Equal("1.0 + 02", textBlock.Text);
        }
Пример #27
0
        public TextBox(ISpriteFont spriteFont)
        {
            _textBlock = new TextBlock(spriteFont)
            {
                Margin     = new Thickness(3, 6),
                Foreground = new SolidColorBrush(Colors.Black),
            };

            _textBlock.Bind(TextBlock.TextProperty, BindingFactory.CreateOneWay(this, TextProperty));

            Content = new Border()
            {
                Background      = new SolidColorBrush(Colors.Gray),
                BorderThickness = new Thickness(1),
                Child           = _textBlock,
            };

            _clickSubscription = Gestures
                                 .Where(gesture => gesture.Type == GestureType.LeftButtonDown)
                                 .Subscribe(HandleClick);
        }
        public void Should_Update_When_Detached_And_Attached_To_Visual_Tree()
        {
            TextBlock target;
            Decorator decorator1;
            Decorator decorator2;
            var       root1 = new TestRoot
            {
                Child = decorator1 = new Decorator
                {
                    Name  = "decorator1",
                    Child = target = new TextBlock(),
                },
            };

            var root2 = new TestRoot
            {
                Child = decorator2 = new Decorator
                {
                    Name = "decorator2",
                },
            };

            var binding = new Binding
            {
                Path           = "Name",
                RelativeSource = new RelativeSource
                {
                    AncestorType = typeof(Decorator),
                }
            };

            target.Bind(TextBox.TextProperty, binding);
            Assert.Equal("decorator1", target.Text);

            decorator1.Child = null;
            Assert.Null(target.Text);

            decorator2.Child = target;
            Assert.Equal("decorator2", target.Text);
        }
Пример #29
0
        public void StringFormat_Should_Not_Be_Applied_When_Binding_To_Non_String_Or_Object()
        {
            var textBlock = new TextBlock
            {
                DataContext = new Class1(),
            };

            var target = new MultiBinding
            {
                StringFormat = "Hello {0}",
                Converter    = new SumOfDoublesConverter(),
                Bindings     =
                {
                    new Binding(nameof(Class1.Foo)),
                    new Binding(nameof(Class1.Bar)),
                }
            };

            textBlock.Bind(Layoutable.WidthProperty, target);

            Assert.Equal(3.0, textBlock.Width);
        }
Пример #30
0
        public void StringFormat_Should_Be_Applied_After_Converter()
        {
            var textBlock = new TextBlock
            {
                DataContext = new Class1(),
            };

            var target = new MultiBinding
            {
                StringFormat = "Foo + Bar = {0}",
                Converter    = new SumOfDoublesConverter(),
                Bindings     =
                {
                    new Binding(nameof(Class1.Foo)),
                    new Binding(nameof(Class1.Bar)),
                }
            };

            textBlock.Bind(TextBlock.TextProperty, target);

            Assert.Equal("Foo + Bar = 3", textBlock.Text);
        }