Browse Source

Allow binding to element without path.

Closes #365.
Steven Kirk 10 years ago
parent
commit
23cc9a2ff4

+ 4 - 0
src/Markup/Perspex.Markup/Data/ExpressionObserver.cs

@@ -204,6 +204,10 @@ namespace Perspex.Markup.Data
                     subscription.Dispose();
                 });
             }
+            else if (_rootObservable != null)
+            {
+                return _rootObservable.Subscribe(observer);
+            }
             else
             {
                 if (_empty == null)

+ 75 - 2
tests/Perspex.Markup.Xaml.UnitTests/Data/BindingTests_ElementName.cs

@@ -10,7 +10,7 @@ namespace Perspex.Markup.Xaml.UnitTests.Data
     public class BindingTests_ElementName
     {
         [Fact]
-        public void Should_Bind_To_Element()
+        public void Should_Bind_To_Element_Path()
         {
             TextBlock target;
             var root = new TestRoot
@@ -44,7 +44,42 @@ namespace Perspex.Markup.Xaml.UnitTests.Data
         }
 
         [Fact]
-        public void Should_Bind_To_Later_Added_Element()
+        public void Should_Bind_To_Element()
+        {
+            TextBlock source;
+            ContentControl target;
+
+            var root = new TestRoot
+            {
+                Child = new StackPanel
+                {
+                    Children = new Controls.Controls
+                    {
+                        (source = new TextBlock
+                        {
+                            Name = "source",
+                            Text = "foo",
+                        }),
+                        (target = new ContentControl
+                        {
+                            Name = "target",
+                        })
+                    }
+                }
+            };
+
+            var binding = new Binding
+            {
+                ElementName = "source",
+            };
+
+            binding.Bind(target, ContentControl.ContentProperty);
+
+            Assert.Same(source, target.Content);
+        }
+
+        [Fact]
+        public void Should_Bind_To_Later_Added_Element_Path()
         {
             TextBlock target;
             StackPanel stackPanel;
@@ -79,5 +114,43 @@ namespace Perspex.Markup.Xaml.UnitTests.Data
 
             Assert.Equal("foo", target.Text);
         }
+
+        [Fact]
+        public void Should_Bind_To_Later_Added_Element()
+        {
+            ContentControl target;
+            StackPanel stackPanel;
+
+            var root = new TestRoot
+            {
+                Child = stackPanel = new StackPanel
+                {
+                    Children = new Controls.Controls
+                    {
+                        (target = new ContentControl
+                        {
+                            Name = "target",
+                        }),
+                    }
+                }
+            };
+
+            var binding = new Binding
+            {
+                ElementName = "source",
+            };
+
+            binding.Bind(target, ContentControl.ContentProperty);
+
+            var source = new TextBlock
+            {
+                Name = "source",
+                Text = "foo",
+            };
+
+            stackPanel.Children.Add(source);
+
+            Assert.Same(source, target.Content);
+        }
     }
 }