diff --git a/Xamarin.Forms.Core/BindingExpression.cs b/Xamarin.Forms.Core/BindingExpression.cs index c733d5bcb..9f6dd0577 100644 --- a/Xamarin.Forms.Core/BindingExpression.cs +++ b/Xamarin.Forms.Core/BindingExpression.cs @@ -152,12 +152,11 @@ namespace Xamarin.Forms if (needsGetter) { object value = property.DefaultValue; - if (part.TryGetValue(current, out value) || part.IsSelf) - { + if (part.TryGetValue(current, out value) || part.IsSelf) { value = Binding.GetSourceValue(value, property.ReturnType); } else - value = Binding.FallbackValue ?? property.DefaultValue; + value = Binding.FallbackValue ?? property.GetDefaultValue(target); if (!TryConvert(ref value, property, property.ReturnType, true)) { diff --git a/Xamarin.Forms.Core/TypedBinding.cs b/Xamarin.Forms.Core/TypedBinding.cs index c8afff741..db71fd945 100644 --- a/Xamarin.Forms.Core/TypedBinding.cs +++ b/Xamarin.Forms.Core/TypedBinding.cs @@ -193,7 +193,7 @@ namespace Xamarin.Forms.Internals Subscribe((TSource)sourceObject); if (needsGetter) { - var value = FallbackValue ?? property.DefaultValue; + var value = FallbackValue ?? property.GetDefaultValue(target); if (isTSource) { try { value = GetSourceValue(_getter((TSource)sourceObject), property.ReturnType); diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh2752.xaml b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh2752.xaml new file mode 100644 index 000000000..dc4fa1a42 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh2752.xaml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/Issues/Gh2752.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh2752.xaml.cs new file mode 100644 index 000000000..ffa7efaa2 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/Issues/Gh2752.xaml.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public class Gh2752VM { + public Gh2752VM Foo { get; set; } + public Gh2752VM Bar { get; set; } + public string Baz { get; set; } + } + public partial class Gh2752 : ContentPage + { + public static readonly BindableProperty MyProperty = + BindableProperty.Create("My", typeof(string), typeof(Gh2752), default(string), defaultValueCreator: b=> "default created value"); + + public string My { + get { return (string)GetValue(MyProperty); } + set { SetValue(MyProperty, value); } + } + + public Gh2752() => InitializeComponent(); + public Gh2752(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Tests + { + [SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices(); + [TearDown] public void TearDown() => Device.PlatformServices = null; + + [TestCase(true), TestCase(false)] + public void FallbcakToDefaultValueCreator(bool useCompiledXaml) + { + var layout = new Gh2752(useCompiledXaml) { BindingContext = null }; + Assert.That(layout.My, Is.EqualTo("default created value")); + } + } + } +} \ No newline at end of file