VisualManuel
VisualManuel

Reputation: 111

Xamarin.Forms Binding works, but text is not showing

I have tried to bind a list of objects to a listview for a long time, but although it works as expected with lists where I don't need to write an itemtemplate (ObservableCollection<string> for example), it does not work with lists where i want an itembinding to a field of an object in a list:

MainPage.xaml.cs:

ExampleList = new ObservableCollection<ExampleItem>()
{
    new ExampleItem() {Showing = "Item 1"},
    new ExampleItem() {Showing = "Item 2"}
};
ListView.ItemsSource = ExampleList;

Mainpage.xaml:

<ListView x:Name="ListView">
      <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Showing}" TextColor="White"></TextCell>
        </DataTemplate>
      </ListView.ItemTemplate>
</ListView>

Although the list items are there(!), the text in the rows just doesn't show up: Binding Result

I already tried this solution, result was the same : Xamarin ListView not displaying any data

ANSWER: It seems that binding doesn't (fully) work with fields, the variables need to be properties!

Upvotes: 8

Views: 4496

Answers (4)

Brice Friha
Brice Friha

Reputation: 223

The issue is an old issue and has been well addressed. But you can also set the ItemSource via XAML. by doing this:

<ListView x:Name="ListView" ItemsSource="{Binding ExampleList}">
      <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Showing}" TextColor="White"></TextCell>
        </DataTemplate>
      </ListView.ItemTemplate>
</ListView>

This can help to avoid this kind of issues 😉

See more here: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding

I hope I was useful somehow 😁

Upvotes: 0

Hristo Alexsiev
Hristo Alexsiev

Reputation: 146

As written in the comments "Binding doesn't work with fields, it needs properties". I am new to xamarin i struggle with it to. But that's the Clear Answer.

Upvotes: 0

Striver
Striver

Reputation: 471

Make sure the items in the ItemsSource implement INotifyPropertyChanged and the setter of each property you are binding to triggers the PropertyChanged event.

That's besides triggering PropertyChanged on the property setter of the ItmsSource.

Upvotes: 2

Esteban Verbel
Esteban Verbel

Reputation: 738

You need to set the ItemsSource to bind the ObservableCollection to the ListView

<ListView ItemsSource="{Binding ExampleList}">
  <ListView.ItemTemplate>
    <DataTemplate>
      <TextCell Text="{Binding Showing}" TextColor="White" />
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

Also remember that when working with Xamarin.Forms it's better to follow the MVVM pattern. You should have the ObservableCollection in a ViewModel class, and set it as the BindingContext on the View

Edit: The ObservableCollection seems to call OnPropertyChange to update the UI on the Add method. Just add the items to the collection after setting the ItemsSource. That should do the trick

ExampleList = new ObservableCollection<ExampleItem>();
ListView.ItemsSource = ExampleList;

ExampleList.Add(new ExampleItem() {Showing = "Item 1"});
ExampleList.Add(new ExampleItem() {Showing = "Item 2"});

Upvotes: 1

Related Questions