Search code examples
c#xamarin.formsbindableproperty

xamarin forms bindable property does not work properly


i am trying to create custom view like below here i am trying to bind a collection from main page to custom view

but here it is not binding properly.

<ContentView.Content>
        <Grid
            x:Name="Layout"
            HorizontalOptions="FillAndExpand"
            VerticalOptions="FillAndExpand">

            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <StackLayout
                Grid.Row="0"
                BackgroundColor="#E0E0E0"
                Padding="20,10">
                <Label Text="{Binding Source={x:Reference ExpandableContentView}, Path=Header}"/>
            </StackLayout>

            <StackLayout
                Grid.Row="1"
                BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">

                <Label Text="{Binding Test}"/>
            </StackLayout>

        </Grid>
    </ContentView.Content>

xmal.cs file

public partial class ExpandableView : ContentView
    {
        public static readonly BindableProperty HeaderProperty = BindableProperty.Create(
            nameof(Header),
            typeof(string),
            typeof(ExpandableView),
            default(string));

        public static readonly BindableProperty SummeryListProperty = BindableProperty.Create(
            nameof(SummeryList),
            typeof(ObservableCollection<SummeryDetailModel>),
            typeof(ExpandableView),
            propertyChanged: CollectionChanged);

        private static void CollectionChanged(BindableObject bindable, object oldValue, object newValue)
        {
        }

        public ExpandableView()
        {
            InitializeComponent();
        }

        public string Header
        {
            get => (string)GetValue(HeaderProperty);
            set => SetValue(HeaderProperty, value);
        }

        public ObservableCollection<SummeryDetailModel> SummeryList
        {
            get => (ObservableCollection<SummeryDetailModel>)GetValue(SummeryListProperty);
            set => SetValue(SummeryListProperty, value);
        }

i am doing the binding in MainPage like below

<commonviews:ExpandableView
            Header="Shipment Details"
            SummeryList="{Binding SummeryCollection}">
        </commonviews:ExpandableView>

My SummeryDetailModel like

public class SummeryDetailModel
    {
        public string Test { get; set; }
    }

here it is not binding the Test value to label.

anything i missed here?


Solution

  • i just figure out this, there was a small mistake that i have done.

    i did not add Itemtemplate DataTemplate for StackLayout BindableLayout

    it is as follows

    <StackLayout
                    Grid.Row="1"
                    Padding="20,10"
                    Spacing="15"
                    BindableLayout.ItemsSource="{Binding Source={x:Reference ExpandableContentView}, Path=SummeryList}">
                    <BindableLayout.ItemTemplate>
                        <DataTemplate>
                            <StackLayout
                                Spacing="10">
                                <Label
                                    Text="{Binding Title}"
                                    FontFamily="{StaticResource LatoRegular}"
                                    FontSize="{StaticResource FontSize14}"
                                    TextColor="{StaticResource PlaceholderColor}"/>
    
                                <Label Text="{Binding Detail}"
                                       FontFamily="{StaticResource LatoRegular}"
                                       FontSize="{StaticResource FontSize14}"
                                       TextColor="{StaticResource PrimaryText}"/>
    
                            </StackLayout>
                        </DataTemplate>
                    </BindableLayout.ItemTemplate>
                </StackLayout>