Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Cascading XAML ContentViews -> binding not working properly

$
0
0

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); }
    }
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>