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

Reuse a viewcell in different list views

$
0
0

Hi All,

I have the following template in a ListView:

<DataTemplate>
    <ViewCell>
        <StackLayout BackgroundColor="#E5E5E5" Orientation="Horizontal" Padding="8,0">
            <Label Text="{Binding .}" FontAttributes="Bold" TextColor="#262626" VerticalOptions="Center"/>
        </StackLayout>
    </ViewCell>
</DataTemplate>

And I'll use the same template for multiple list views so I created a ViewCell with the following content:

<?xml version="1.0" encoding="utf-8" ?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      x:Class="PpgApp.Client.Views.Extensions.MenuItemCell">
    <StackLayout BackgroundColor="#E5E5E5" Orientation="Horizontal" Padding="8,0">
        <Label Text="{Binding Text}" FontAttributes="Bold" TextColor="#262626" VerticalOptions="Center"/>
    </StackLayout>
</ViewCell>

public partial class MenuItemCell: ViewCell
{
    public MenuItemCell()
    {
        InitializeComponent();
        BindingContext = this;
    }

    public static readonly BindableProperty TextProperty =
        BindableProperty.Create("Text", typeof(string), typeof(MenuItemCell));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
}

I use it like this:

<local:MenuItemCell Text="Foo" />

My issue is that the data binding isn't working, i.e., the elements are rendered but the labels don't have any text.

What am I missing?

Cheers!


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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