<Import_RootNamespace>Xamarin.Forms.Controls.Issues</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
- <Compile Include="$(MSBuildThisFileDirectory)Issue2894.cs" />
+ <Compile Include="$(MSBuildThisFileDirectory)Issue2894.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3524.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2004.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3333.cs" />
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
-</Project>
+</Project>
\ No newline at end of file
--- /dev/null
+using System.Collections;
+using System.Linq;
+using NUnit.Framework;
+
+
+namespace Xamarin.Forms.Core.UnitTests
+{
+ [TestFixture]
+ public class TitleViewUnitTests : BaseTestFixture
+ {
+ [SetUp]
+ public override void Setup()
+ {
+ base.Setup();
+ Device.PlatformServices = new MockPlatformServices();
+ }
+
+ [TearDown]
+ public override void TearDown()
+ {
+ base.TearDown();
+ Device.PlatformServices = null;
+ }
+
+ [Test]
+ public void BindingContextPropagatesFromParent()
+ {
+ NavigationPage navigationPage = new NavigationPage();
+ var image1 = new Image();
+ image1.SetBinding(Image.SourceProperty, "ImageSource");
+ var page = new ContentPage()
+ {
+ Content = new Label()
+ };
+
+ var title = new Label() { Text = "Failed" };
+ title.SetBinding(Label.TextProperty, "Title");
+
+ var layout = new StackLayout()
+ {
+ Orientation = StackOrientation.Horizontal,
+ Children =
+ {
+ title,
+ image1
+ }
+ };
+
+ page.SetValue(NavigationPage.TitleViewProperty, layout);
+ navigationPage.PushAsync(page);
+
+ var model = new Model();
+ navigationPage.BindingContext = new Model();
+ Assert.AreEqual(model.Title, title.Text);
+
+ string success = "Success";
+ page.BindingContext = new Model() { Title = success };
+ Assert.AreEqual(success, title.Text);
+ navigationPage.BindingContext = new Model() { Title = "Failed" };
+ Assert.AreEqual(success, title.Text);
+ }
+
+ public class Model
+ {
+ public string Title { get; set; } = "Binding Working";
+ public string ImageSource { get; } = "coffee.png";
+ }
+ }
+}
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Compile Include="MenuUnitTests.cs" />
<Compile Include="RegionTests.cs" />
<Compile Include="SpanTests.cs" />
+ <Compile Include="TitleViewUnitTests.cs" />
<Compile Include="TemplatedViewUnitTests.cs" />
<Compile Include="TemplatedPageUnitTests.cs" />
<Compile Include="ContentFormUnitTests.cs" />
<LogicalName>Images/crimson.jpg</LogicalName>
</EmbeddedResource>
</ItemGroup>
-</Project>
+</Project>
\ No newline at end of file
if (oldValue == newValue)
return;
- if (oldValue != null)
+ if(bindable is Page page)
{
- var oldElem = (View)oldValue;
- oldElem.Parent = null;
+ page.SetTitleView((View)oldValue, (View)newValue);
}
-
- if (newValue != null && bindable != null)
+ else if (oldValue != null)
{
- var newElem = (View)newValue;
- newElem.Parent = (Page)bindable;
+ var oldElem = (View)oldValue;
+ oldElem.Parent = null;
}
}
ReadOnlyCollection<Element> _logicalChildren;
+ View _titleView;
+
public Page()
{
var toolbarItems = new ObservableCollection<ToolbarItem>();
{
SetInheritedBindingContext(toolbarItem, BindingContext);
}
+
+ if(_titleView != null)
+ SetInheritedBindingContext(_titleView, BindingContext);
}
protected virtual void OnChildMeasureInvalidated(object sender, EventArgs e)
{
return _platformConfigurationRegistry.Value.On<T>();
}
+
+ internal void SetTitleView(View oldTitleView, View newTitleView)
+ {
+ if (oldTitleView != null)
+ oldTitleView.Parent = null;
+
+ if (newTitleView != null)
+ newTitleView.Parent = this;
+
+ _titleView = newTitleView;
+ }
}
}