Cascading XAML ContentViews -> binding not working properly

Hello,
When I use cascading XAML ContentViews, it seems that some XAML bindings don't work properly.
I wrote some lines to show the problem.
It seems obvious that the following code should display 'Hello!!' twice.
But 'Hello!!' appears only once (label of View1 only). In order to bind properly the View2 label, I have to define the binding in code-behind (uncomment the 'SetBinding' in View1.cs).
Why doesn't the binding work when defined in XAML? What am I missing?

Here is the example:

MainPage.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App1"
             x:Class="App1.MainPage">

  <local:View1 Caption="Hello!!" VerticalOptions="Center" HorizontalOptions="Center" />
</ContentPage>

MainPage.cs:

namespace App1 {
public partial class MainPage : ContentPage {
    public MainPage() {
        InitializeComponent();
    }
}

View1.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App1"
             x:Class="App1.View1">

  <StackLayout>
    <Label Text="{Binding Caption}" />
    <local:View2 x:Name="view2" Caption="{Binding Caption}" />
  </StackLayout>
</ContentView>

View1.cs:

public partial class View1 : ContentView {
    public static readonly BindableProperty CaptionProperty = BindableProperty.Create("Caption", typeof(string), typeof(View1), null);

    public View1() {
        InitializeComponent();
        BindingContext = this;
        //view2.SetBinding(View2.CaptionProperty, new Binding("Caption", source: this)); //already definied in XAML but doesn't work ???
    }

    public string Caption {
        get { return (string)GetValue(CaptionProperty); }
        set { SetValue(CaptionProperty, value); }
    }
}

View2.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App1.View2">

  <Label Text="{Binding Caption}" />
</ContentView>

View2.cs:

public partial class View2 : ContentView {
    public static readonly BindableProperty CaptionProperty = BindableProperty.Create("Caption", typeof(string), typeof(View2), null);

    public View2() {
        InitializeComponent();
        BindingContext = this;
    }

    public string Caption {
        get { return (string)GetValue(CaptionProperty); }
        set { SetValue(CaptionProperty, value); }
    }
}

Best Answer

  • englebertnicolasenglebertnicolas LU ✭✭
    Accepted Answer

    Yes. There is actually no problem with Xamarin. The above code is wrong.

    The line:
    BindingContext = this;
    in the file view2.cs breaks the binding that is defined at the upper level in view1.xaml:
    <local:View2 x:Name="view2" Caption="{Binding Caption}" />

    To fix the issue:
    1. Remove BindingContext = this; in View2.cs
    2. And specify the binding like this in View2.xaml:

    <ContentView ... x:Name="view2">
        <Label Text="{Binding Caption, Source={x:Reference view2}}" />
     </ContentView>
    

Answers

  • flchauxflchaux FRMember ✭✭

    I encounter the same problem. Cascading binding doesn't work. Do you find a solution ?

  • englebertnicolasenglebertnicolas LUMember ✭✭
    Accepted Answer

    Yes. There is actually no problem with Xamarin. The above code is wrong.

    The line:
    BindingContext = this;
    in the file view2.cs breaks the binding that is defined at the upper level in view1.xaml:
    <local:View2 x:Name="view2" Caption="{Binding Caption}" />

    To fix the issue:
    1. Remove BindingContext = this; in View2.cs
    2. And specify the binding like this in View2.xaml:

    <ContentView ... x:Name="view2">
        <Label Text="{Binding Caption, Source={x:Reference view2}}" />
     </ContentView>
    
  • flchauxflchaux FRMember ✭✭

    Thank you very much englebertnicolas ! I found the source of the issue few hours ago but your message confirm my doubt.

Sign In or Register to comment.