How to data bind in xaml

Hi

I've tried to test out PCL with XAML file that should share UI for cross-platform..

The problem is i dont know how to the data properly.

public class Employee { public string DisplayName { get; set; } }

public partial class EmployeeListPage : ContentPage { public ObservableCollection<Employee> employees = new ObservableCollection<Employee>(); public EmployeeListPage() { employees.Add(new Employee { DisplayName = "Rob Finnerty" }); employees.Add(new Employee { DisplayName = "Bill Wrestler" }); employees.Add(new Employee { DisplayName = "Dr. Geri-Beth Hooper" }); employees.Add(new Employee { DisplayName = "Dr. Keith Joyce-Purdy" }); employees.Add(new Employee { DisplayName = "Sheri Spruce" }); employees.Add(new Employee { DisplayName = "Burt Indybrick" }); } }

Name of XAML file is Page1.
`

    <ListView ItemSource="{Binding employees}" x:Name="emView">
      <ListView.ItemTemplate>
        <DataTemplate>
          <TextCell Text="{Binding DisplayName}" />                      
        </DataTemplate>                     
      </ListView.ItemTemplate>
    </ListView>`

For make it run and show the list i did this in the Page1.xaml.cs and deleting "ItemSoruce="{Binding employees}"
public Page1() { InitializeComponent(); var test = new EmployeeListPage(); emView.ItemsSource = test.employees; } }

Best Answer

Answers

  • FuriousFurious USMember

    I just saw the spelling mistake: ItemSource, changed it to ItemsSource.. This makes it run without crashing but it doesn't show anything..

  • Martijn00Martijn00 NLInsider, University ✭✭✭

    There are some great examples available for Xamarin.Forms using MvvmCross: https://github.com/MvvmCross/MvvmCross-Forms

  • NMackayNMackay GBInsider, University mod

    I didn't get that far but also check out MVVM Light.

    There's a sample here to get started or just PM me if you want another sample.

    http://blog.galasoft.ch/posts/2014/07/using-xamarin-forms-with-mvvmlight/

  • FuriousFurious USMember

    Hey guys, thanks for the reply! I got it fixed, im gonna show the code here later if anyone else wanna runs into the same problem.

Sign In or Register to comment.