Guido Magrin Guido Magrin - 3 months ago 77
C# Question

Xamarin Forms listview databinding

I am currently following this guide:

http://www.morganskinner.com/2015/01/xamarin-forms-contacts-search.html

I am willing to make a contacts page in my app, like the one showed in the pictures there, still something is not working out as it should for me:

I wrote/copied the XAML:



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

<SearchBar x:Name="search" Placeholder="Search"/>

<ListView x:Name="ProvaView" Grid.Row="1" ItemsSource="{Binding FilteredContacts}" IsGroupingEnabled="true" GroupDisplayBinding="{Binding Key}" GroupShortNameBinding="{Binding Key}">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" TextColor="Black" Detail="{Binding PhoneNumber}" DetailColor="Gray">
</TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

</Grid>


and now I am trying to "display something", not dinamically loaded (yet), from a list of data.

I declared a class like this:

public class Contact
{
public string Name { get; set; }
public string PhoneNumber { get; set; }
}
}


and now the code behind the xaml is this:

public partial class ContactsPage : MasterDetailPage
{
public List<Contact> FilteredContacts = new List<Contact>
{
new Contact
{
Name = "Guido",
PhoneNumber = "3292773477"
},
new Contact
{
Name = "Luca",
PhoneNumber = "3472737445"
},
new Contact
{
Name = "Luca",
PhoneNumber = "3472737445"
}
};

public ContactsPage()
{
InitializeComponent();
ProvaView.ItemsSource = FilteredContacts;
}
}


When I launch the app, all I get anyway is an empty listview, with 3 fields but nothing in it.

Where am I doing wrong?

Also, straight from the page where I am working off, I can't understand the meaning of these Fields associated to the listview:

GroupDisplayBinding="{Binding Key}"
GroupShortNameBinding="{Binding Key}"

and as much as I could look around on the web, I coudln't find any reference to both of them.

Answer

Most likely the problem is with the way you declared FilteredContacts, it should be a property, and not a field. The reflection mechanism used in binding only searches for properties, fields are not included.