1

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.

2 Answers2

1

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

Ivan Bukashkin
  • 674
  • 3
  • 11
1

We have looked into your code snippet and we have found that you have binded the underlying collection to ItemTemplate property instead of ItemsSource property. Further to bind the underlying collection you have to set your ViewModel(i.e. BandInfoRepository) as BindingContext for ContentPage. Please refer the below code snippets to know how to set BindingContext for your page and also to bind the underlying collection into the ItemsSource property.

Code Example:[XAML]

<ContentPage>
  <ContentPage.BindingContext>
    <local:BandInfoRepository/>
  </ContentPage.BindingContext>

  <ContentPage.Content>
    <listView:SfListView x:Name="listView" ItemSize="70" ItemsSource="{Binding BandInfo}" >

      <listView:SfListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <Grid x:Name="grid" RowSpacing="1">
                <Grid.RowDefinitions>
                  <RowDefinition Height="*" />
                  <RowDefinition Height="1" />
                </Grid.RowDefinitions>
                <Grid RowSpacing="1">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>
                  <Image Source="{Binding BandImage}"
     VerticalOptions="Center"
     HorizontalOptions="Center"
     HeightRequest="50" Aspect="AspectFit"/>
                  <Grid Grid.Column="1"
    RowSpacing="1"
    Padding="10,0,0,0"
    VerticalOptions="Center">
                    <Label Text="{Binding ContactName}"/>
                  </Grid>
                </Grid>
                <StackLayout Grid.Row="1" BackgroundColor="Gray" HeightRequest="1"/>
              </Grid>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </listView:SfListView.ItemTemplate>
    </listView:SfListView>
  </ContentPage.Content>
</ContentPage>

For your assistance, we have attached the working sample link below.

Sample link: http://www.syncfusion.com/downloads/support/directtrac/186932/ze/ListViewSample905947849

kyuubi
  • 71
  • 4