Forum Xamarin.Forms

How to open specific page when selecting specific item on a listview with DataBiding?

DavidFreireDavidFreire USMember ✭✭
edited January 2017 in Xamarin.Forms

I have a ListView that has item source of a local data base like this:
XAML

    <ListView x:Name="MylistView" >
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout Padding="15" Orientation="Horizontal">
                        <Label Text="{Binding Id}" FontAttributes="Bold" TextColor="Gray" />
                        <Label Text="-" FontAttributes="Bold" TextColor="Gray" />
                        <Label Text="{Binding Titulo}" FontAttributes="Bold" TextColor="Gray" />                  
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

C# code behind

        List<MyListView> pplList = await App.PersonRepo.GetAllPeopleAsync();

        ObservableCollection<THinos> pplCollection = new ObservableCollection<THinos>(pplList);
        HinoListView.ItemsSource = pplCollection;

So i want that when i click on an item on the listview he open a new page with the property data of the Id that his taking of the database, something like "See more" button that opens another page with the rest of the content...

I'm kinda new on Xamarin forms so any help would be great...

Best Answer

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    edited January 2017

    there are 2 ways.
    You can use ItemSelected event

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Selection_Taps

    Or you can use SelectedItem property. When SelectedItem is set, you can open the page.

    Something like (with binding, INotifyPropertyChanged and so on)

    public MyModel SelectedItem{
        get {return _selectedItem};
        set {
            _selectedItem = value;
            if(_selectedItem != null)
                Application.Current.MainPage.Navigation.PushAsync(new PageDetail(_selectedItem));
        }
    }
    
  • DavidFreireDavidFreire USMember ✭✭

    @AlessandroCaliaro said:
    there are 2 ways.
    You can use ItemSelected event

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/interactivity/#Selection_Taps

    Or you can use SelectedItem property. When SelectedItem is set, you can open the page.

    Something like (with binding, INotifyPropertyChanged and so on)

    public MyModel SelectedItem{
    get {return _selectedItem};
    set {
    _selectedItem = value;
    if(_selectedItem != null)
    Application.Current.MainPage.Navigation.PushAsync(new PageDetail(_selectedItem));
    }
    }

    I didn't understand your code...
    I have a page with the listview and another page that i want to show the description of the selected item, can you make a exemple for me try to understand?

  • DavidFreireDavidFreire USMember ✭✭

    @AlessandroCaliaro said:
    in your xaml you should have a SelectedItem property, right?
    I don't use xaml... in c# it is something like

    listView.SetBinding(ListView.SelectedItemProperty, "SelectedItem");

    then in your ViewModel you should have something like

    private MyModel _selectedItem {get;set;}

    public MyModel SelectedItem {

      get {
          return _selectedItem;
      }
    
      set {
          _selectedItem = value;
    
          // When your item is selected, you can open a new "PageDetail" and pass the value
          Application.Current.MainPage.Navigation.PushAsync (new PageDetail(_selectedItem)); // If you are in a Navigation page
      }
    

    It's clear?

    Otherwise, use ItemSelected or ItemTapped events...

    I still can not do it... just to simplify i want something like this guy said...
    https://forums.xamarin.com/discussion/21080/swiping-between-pages-carousel-page

    For example i have a listview that takes all e-mails from a binding with the database and i want that when i click on any e-mail he will open the content of the e-mail

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    take a look there. Maybe useful..

    https://github.com/acaliaro/TestBindingWithListView

  • DavidFreireDavidFreire USMember ✭✭

    That's not what i want... So, I have a page with a listview data binding and a page carousel with the same data binding... I want that when i click on a item on the list i navigate to the properly children of the carousel page.

    Just it, but i can't figure out how to do it...

Sign In or Register to comment.