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

Binding does not update view when BindingContext is page itself #3994

Closed
Maxima078 opened this issue Oct 4, 2018 · 6 comments · Fixed by #4028

Comments

@Maxima078
Copy link

commented Oct 4, 2018

Description

I just noticed a strange behaviour when BindingContext is set to a page itself. If I use a binding such as Text="{Binding BindingContext.TestLabel, Source={x:Reference myPage}}" in a Label for instance, Text will never be updated.
Text="{Binding TestLabel}" is OK.
Text="{Binding TestLabel, Source={x:Reference myPage}}" is OK
Note that this issue does not exist when BindingContext is set to something else than the page itself.

Steps to Reproduce

  1. Set BindingContext = this in a page
  2. Put a Label with Text bound to a page prop using BindingContext Text="{Binding BindingContext.TestLabel, Source={x:Reference myPage}}"
  3. At runtime update this prop

Expected Behavior

Text is updated

Actual Behavior

Text is not updated

Basic Information

  • Version with issue: 3.2.0.839982
  • Android: 7.0

Reproduction Link

Xamarin.samples.navigation.bindingcontext.zip

@kingces95

This comment has been minimized.

Copy link
Member

commented Oct 4, 2018

Reproduces as described.

@kingces95 kingces95 moved this from New to Ready For Work in Triage Oct 4, 2018

@StephaneDelcroix

This comment has been minimized.

Copy link
Member

commented Oct 5, 2018

mmm, I can not reproduce in a unitest...

[Test]
//https://github.com/xamarin/Xamarin.Forms/issues/3994
public void INPCOnBindingWithSource()
{
	var vm = new MockViewModel{Text="Foo"};
	var label = new Label();
	var layout = new StackLayout{
		Children={label},
		BindingContext = vm,
	};
	label.SetBinding(Label.TextProperty, new Binding("BindingContext.Text", source:layout));
	Assert.That(label.Text, Is.EqualTo("Foo"));

	vm.Text="Bar";
	Assert.That(label.Text, Is.EqualTo("Bar"));
}
@AmrAlSayed0

This comment has been minimized.

Copy link

commented Oct 6, 2018

I think what he means is this:

[Test]
public void INPCWhenPageBindingContextIsItself()
{
	var page = new ContentPageWithTextProperty();
    page.Text = "Foo";
    page.BindingContext = page;
	var label = new Label();
	page.Content = label;
	label.SetBinding(Label.TextProperty, new Binding("BindingContext.Text", source:page));
	Assert.That(label.Text, Is.EqualTo("Foo"));

	page.Text="Bar";
	Assert.That(label.Text, Is.EqualTo("Bar"));
}
@RodgerLeblanc

This comment has been minimized.

Copy link

commented Oct 7, 2018

@AmrAlSayed0 is right, here's a modifed version of his unit test that you can run and fails. I'm using the Page.Title property instead of creating a TestLabel bindable property.

[Test]
//https://github.com/xamarin/Xamarin.Forms/issues/3994
public void INPCWhenPageBindingContextIsItself()
{
    ContentPage page = new ContentPage();
    page.Title = "Foo";
    page.BindingContext = page;
    Label label = new Label();
    page.Content = label;
    label.SetBinding(Label.TextProperty, new Binding("BindingContext.Title", source: page));
    Assert.That(label.Text, Is.EqualTo("Foo"));

    page.SetValue(Page.TitleProperty, "Bar");
    Assert.That(label.Text, Is.EqualTo("Bar"));
}
StephaneDelcroix added a commit that referenced this issue Oct 7, 2018

@samhouts samhouts added this to In Progress in v3.4.0 Oct 8, 2018

@samhouts samhouts removed this from Ready For Work in Triage Oct 9, 2018

@StephaneDelcroix

This comment has been minimized.

Copy link
Member

commented Nov 20, 2018

@RodgerLeblanc I made your binding works in #4028. However, your binding path is wrong. You should bind to Title, not BindingContext.Title, or worse, BindingContext.BindingContext.BindingContext.Title

@samhouts samhouts added this to In Progress in v3.6.0 Nov 20, 2018

@samhouts samhouts removed this from In Progress in v3.4.0 Nov 20, 2018

StephaneDelcroix added a commit that referenced this issue Nov 21, 2018

@samhouts samhouts moved this from In Progress to Done in v3.6.0 Nov 26, 2018

@samhouts samhouts removed this from Done in v3.6.0 Jan 3, 2019

@mattisam

This comment has been minimized.

Copy link

commented Jan 10, 2019

This issue still exists. It just happens to work, because in description and in reproduction the BindingContext is set to 'this', where the TestLabel is located, but if binding context is set to anything else e.g. view model or null, the Source={x:Reference myPage}} still doesn't work. (Tested with 4.0.0-pre1)

Here is an example:
BindingTest.zip

edit: Since this is already closed, I want to draw your attention: @StephaneDelcroix, @samhouts

@samhouts samhouts added this to In Progress in v3.5.0 Jan 11, 2019

@samhouts samhouts moved this from In Progress to Done in v3.5.0 Jan 11, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
6 participants
You can’t perform that action at this time.