Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting binding on Label doesn't set text to incoming binding #16410

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public FreezeMe()

#if UITEST
[Test]
[Microsoft.Maui.Controls.Compatibility.UITests.FailsOnMauiAndroid]
[Microsoft.Maui.Controls.Compatibility.UITests.FailsOnMauiIOS]
public void ListViewDoesntFreezeApp()
{
RunningApp.Tap(x => x.Marked(NavigateToPage));
Expand Down
12 changes: 8 additions & 4 deletions src/Controls/src/Core/BindableObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,11 @@ public void RemoveBinding(BindableProperty property)
public void SetBinding(BindableProperty targetProperty, BindingBase binding)
=> SetBinding(targetProperty, binding, SetterSpecificity.FromBinding);

//FIXME, use specificity
internal void SetBinding(BindableProperty targetProperty, BindingBase binding, SetterSpecificity specificity)
{
if (targetProperty == null)
throw new ArgumentNullException(nameof(targetProperty));


if (targetProperty.IsReadOnly && binding.Mode == BindingMode.OneWay)
{
Application.Current?.FindMauiContext()?.CreateLogger<BindableObject>()?.LogWarning($"Cannot set the a OneWay Binding \"{targetProperty.PropertyName}\" because it is readonly.");
Expand All @@ -228,6 +226,14 @@ internal void SetBinding(BindableProperty targetProperty, BindingBase binding, S

var context = GetOrCreateContext(targetProperty);

//if the value is manually set (has highest specificity than FromBinding), we reassign the specificity so it'll get replaced when the binding is applied
if (context.Values.Last().Key.CompareTo(SetterSpecificity.FromBinding) > 0)
{
var kvp = context.Values.Last();
context.Values.Remove(kvp.Key);
context.Values.Add(SetterSpecificity.FromBinding, kvp.Value);
}

context.Binding?.Unapply();
context.BindingSpecificity = specificity;

Expand All @@ -237,8 +243,6 @@ internal void SetBinding(BindableProperty targetProperty, BindingBase binding, S
targetProperty.BindingChanging?.Invoke(this, oldBinding, binding);

binding.Apply(BindingContext, this, targetProperty, false, specificity);


}

/// <include file="../../docs/Microsoft.Maui.Controls/BindableObject.xml" path="//Member[@MemberName='SetInheritedBindingContext']/Docs/*" />
Expand Down
9 changes: 9 additions & 0 deletions src/Controls/tests/Core.UnitTests/LabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ public void EntryCellYAlignBindingMatchesVerticalTextAlignmentBinding()
Assert.Equal(TextAlignment.End, labelVerticalTextAlignment.VerticalTextAlignment);
}

[Fact]
public void OriginalLabelTextNotUpdatingAfterBindingIsSet()
{
var label = new Label() { Text = "sassifrass" };
label.BindingContext = 1;
label.SetBinding(Label.TextProperty, ".");
Assert.Equal("1", label.Text);
}

sealed class ViewModel : INotifyPropertyChanged
{
TextAlignment horizontalAlignment;
Expand Down