Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Binding custom view

$
0
0

Hello,

I am having some troubles to use a customized view and bindings...
Actually, I want to create a custom grid. So, I created a new class that extends Grid.

namespace Listes.Customized
{
    class ListItem : Grid
    {
        public ListItem() : base()
        {
            var name = new Label { Text = "pwet" };
            var layout = new StackLayout { Orientation = StackOrientation.Horizontal };
            layout.Children.Add(name);
            this.Children.Add(layout);
        }

        public Liste Item
        {
            get { return (Liste)GetValue(ItemProperty); }
            set { SetValue(ItemProperty, value); }
        }

        public static readonly BindableProperty ItemProperty = BindableProperty.Create<ListItem, Liste>(p => p.Item, new Liste());

        protected override void OnPropertyChanged(string propertyName = null)
        {
            var name = new Label { Text = Item.Name };
            var layout = new StackLayout { Orientation = StackOrientation.Horizontal };
            if (layout.Children != null)
            {
                layout.Children.Add(name);
                if (this.Children != null)
                    this.Children.Add(layout);
            }
        }
    }
}

And I use it in my xaml this way :

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                       x:Class="Listes.Main"
                        xmlns:localConverters="clr-namespace:Listes.Converters;assembly=Listes"
                        xmlns:localCustomized="clr-namespace:Listes.Customized;assembly=Listes"
             Title="Mes Listes">
    <ContentPage.Resources>
        <ResourceDictionary>
            <localConverters:PercentageConverter x:Key="PercentageConverter" />
            <localConverters:ColorPercentageConverter x:Key="ColorPercentageConverter" />
            <localCustomized:ListItem x:Key="ListItem" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ListView x:Name="Listes" ItemsSource="{Binding Listes}" ItemTapped="OnItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <ViewCell.View>
                        <localCustomized:ListItem Item="{Binding Items}"/>
                    </ViewCell.View>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

My list has two items, and when I launch the application, I do not see the Item names in the list view, but only "pwet".

Anyone have any idea about how to display the right name property ?

Thank you,
Riad.


Viewing all articles
Browse latest Browse all 58056

Trending Articles



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