Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to data bind in xaml

$
0
0

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;           
        }
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles