Search This Blog

Wednesday, 29 June 2016

ListView inMVVM

XamlPage(View)

<ContentPage.Content>
    <StackLayout Orientation="Vertical">
      <ListView x:Name="lstPersons" ItemsSource="{Binding PersonsList}" >
        <ListView.ItemTemplate>
          <DataTemplate>
            <ViewCell>
              <StackLayout Orientation="Horizontal">
                <Label Text="{Binding PersonName}" VerticalOptions="Start" HorizontalOptions="StartAndExpand"/>
                <Label Text="{Binding PersonAddress}" VerticalOptions="Start" HorizontalOptions="StartAndExpand"/>
                <Label Text="{Binding PersonId}" VerticalOptions="Start" HorizontalOptions="StartAndExpand"/>
              </StackLayout>
            </ViewCell>
          </DataTemplate>
        </ListView.ItemTemplate>
      </ListView>
    </StackLayout>
  </ContentPage.Content>

Class(Model)

class Person
    {
        public string PersonName { set; get; }
        public string PersonAddress { set; get; }
        public int PersonId { set; get; }
    }


ViewModelClass


 class HomePageViewModel
    {

        public HomePageViewModel()
        {
            bindPersonsList();
        }


       private void bindPersonsList()
        {
            _PersonsList.Clear();
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
            _PersonsList.Add(new Person { PersonName = "Venakt", PersonAddress = "Hyderabad", PersonId = 2 });
            _PersonsList.Add(new Person { PersonName = "Chiru", PersonAddress = "Kanigiri", PersonId = 3 });
            _PersonsList.Add(new Person { PersonName = "Subbu", PersonAddress = "Chirala", PersonId = 4 });
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
            _PersonsList.Add(new Person { PersonName = "Ramesh", PersonAddress = "Kandukur", PersonId = 1 });
        }



        private ObservableCollection<Person> _PersonsList = new ObservableCollection<Person>();
        public ObservableCollection<Person> PersonsList
        {
            set
            {
                _PersonsList = value;
            }
            get
            {
                return _PersonsList;
            }
        }
    }

No comments:

Post a Comment