Search code examples
xamllistviewxamarindata-bindingsyncfusion

Syncfusion Xamarin Listview not displaying any items


I want to insert a syncfusion linearlayout listview, but for some reason it's not displaying any items/data, and I'm not getting any errors at the same time, I tried changing binding syntax and a couple things, but I cannot seem to get it right.

This is my xaml:

            <syncfusion:SfListView x:Name="listView"
                ItemTemplate="{Binding Source={local2:BandInfoRepository}, Path=BandInfo, Mode=TwoWay}"
                ItemSize="100"
                AbsoluteLayout.LayoutBounds="1,1,1,1" 
                AbsoluteLayout.LayoutFlags="All" >
                <syncfusion:SfListView.ItemTemplate>
                    <DataTemplate>
                        <Grid RowSpacing="0" Padding="0,12,8,0" ColumnSpacing="0" Margin="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="1" />
                            </Grid.RowDefinitions>
                            <Grid RowSpacing="0" Padding="8,0,8,10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Image Source="{Binding Path=BandImage}"
                                    Grid.Column="0"
                                    Grid.Row="0"
                                    HeightRequest="80"
                                    WidthRequest="70"
                                    HorizontalOptions="Start"
                                    VerticalOptions="Start"
                                />
                                <StackLayout Orientation="Vertical" 
                                    Padding="5,-5,0,0"
                                    VerticalOptions="Start"
                                    Grid.Row="0"
                                    Grid.Column="1">
                                    <Label Text="{Binding Path=BandName}" 
                                        FontAttributes="Bold"
                                        FontSize="16"
                                        BackgroundColor="Green"
                                        TextColor="#000000" />
                                    <Label Text="{Binding Path=BandDescription}"
                                        Opacity="0.54"
                                        BackgroundColor="Olive"
                                        TextColor="#000000"
                                        FontSize="13" />
                                </StackLayout>
                            </Grid>
                            <BoxView Grid.Row="1" 
                                HeightRequest="1"
                                Opacity="0.75"
                                BackgroundColor="#CECECE" />
                        </Grid>
                    </DataTemplate>
                </syncfusion:SfListView.ItemTemplate>
            </syncfusion:SfListView>

And this is the class where I'm getting the data from:

public class BandInfo : INotifyPropertyChanged
{
    private string bandName;
    private string bandDesc;
    private ImageSource _bandImage;
    public string BandName
    {
        get { return bandName; }
        set
        {
            bandName = value;
            OnPropertyChanged("BandName");
        }
    }

    public string BandDescription
    {
        get { return bandDesc; }
        set
        {
            bandDesc = value;
            OnPropertyChanged("BandDescription");
        }
    }

    public ImageSource BandImage
    {
        get { return _bandImage; }
        set
        {
            _bandImage = value;
            OnPropertyChanged("BandImage");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string name)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

And just in case, this is how I'm filling the collection (BandInfoRepository.cs):

public class BandInfoRepository
{
    private ObservableCollection<BandInfo> bandInfo;

    public ObservableCollection<BandInfo> BandInfo
    {
        get { return bandInfo; }
        set { this.bandInfo = value; }
    }

    public BandInfoRepository()
    {
        GenerateBookInfo();
    }

    internal void GenerateBookInfo()
    {
        string[] BandNames = new string[] {
            "Nirvana",
            "Metallica",
            "Frank Sinatra"
        };

        string[] BandDescriptions = new string[] {
            "Description",
            "Description",
            "Description"
        };

        bandInfo = new ObservableCollection<BandInfo>();

        for (int i = 0; i < BandNames.Count(); i++)
        {
            var band = new BandInfo()
            {
                BandName = BandNames[i],
                BandDescription = BandDescriptions[i],
                BandImage = ImageSource.FromResource("Lim.Images.Image" + i + ".png")
            };
            bandInfo.Add(band);
        }
    }
}

I hope you guys can help me out as I've been stuck with this for a while now. Thanks in advance.


Solution

  • Looks like you unintentionally bind ItemTemplate twice and not bind any ItemsSource even once.