Maximus
Maximus

Reputation: 1299

Xamarin Forms - My ContentPresenter won't show its Content

I'm still getting used to Xamarin Forms, so I have the following control called PopupFrame:

PopupFrame.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PopupFrame : ContentView
{
    public static readonly BindableProperty PopupContentProperty =
        BindableProperty.Create(nameof(PopupContent), typeof(View), typeof(PopupFrame));

    public View PopupContent
    {
        get { return (View)GetValue(PopupContentProperty); }
        set { SetValue(PopupContentProperty, value); }
    }

    public PopupFrame()
    {
        InitializeComponent();
    }
}

PopupFrame.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="TestApp.Core.Controls.PopupFrame">
    <Frame>
        <StackLayout>
            <Label Text="--- TEST TITLE ---" />

            <ContentPresenter Content="{TemplateBinding PopupContent}" />
        </StackLayout>
    </Frame>
</ContentView>

In my view:

<popCtl:PopupFrame HorizontalOptions="Center"
                   VerticalOptions="Center">
    <popCtl:PopupFrame.PopupContent>
        <ListView x:Name="ListUsers">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.View>
                            <Label Text="{Binding Name}"
                                   HorizontalOptions="CenterAndExpand"
                                   VerticalOptions="Center" />
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        </popCtl:PopupFrame.PopupContent>
</popCtl:PopupFrame>

So what's happening is that When the ContentView control shows, only the Label (with the text -- TEST TITLE -- is displayed, but not the ListView).

I've also tried replacing the ContentPreseter with a ContentView, but same result: my ListView doesn't not show. And I made sure that data does in fact exist in the ItemsSource of the ListView (set in code-behind).

Is my ContentView setup wrong??

Upvotes: 3

Views: 4031

Answers (1)

Sharada
Sharada

Reputation: 13601

TemplateBinding can only be used to bind from inside a control-template. In order for your binding to work - you can use ReferenceExtension to refer to parent control.

For ex, update your binding as following:

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TestApp.Core.Controls.PopupFrame"
             x:Name="_parent">
    <Frame>
        <StackLayout>
            <Label Text="--- TEST TITLE ---" />

            <ContentPresenter 
                 Content="{Binding Path=PopupContent, Source={x:Reference _parent}}" />
        </StackLayout>
    </Frame>
</ContentView>

Upvotes: 4

Related Questions