Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

how Select by default the first item into the listview xaml??

I have a dynmic list view i want to show the firs item selected by default!!!!!


..

Tagged:

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you have to Bind the ListView's SelectedItem property (I think with BindingMode.TwoWay) to a SelectedItem in your VM. Set in your VM

    SelectedItem = _myList[0];
    
  • seanydaseanyda GBMember ✭✭✭✭✭
    edited April 2017

    SelectedItem = _myList.FirstOrDefault();

  • YoussefRafrafiYoussefRafrafi USMember ✭✭

    Ihave this listview ,can i select an item by default in the code behind??

  • AlexDunnAlexDunn USMember ✭✭✭

    If you're not using MVVM and bindings (which it sounds like you aren't), then you can try this:

    var myCollection = new List<Item>();
    ...
    MyListView.ItemsSource = myCollection;
    MyListView.SelectedItem = ((List<Item>)MyListView.ItemsSource).FirstOrDefault();
    
  • YoussefRafrafiYoussefRafrafi USMember ✭✭
    edited April 2017

    @AlexDunn @AlessandroCaliaro i'm using MVVM :(

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    You have had 3 answers. Have you tried one?
  • YoussefRafrafiYoussefRafrafi USMember ✭✭

    @AlessandroCaliaro yes but it's not solved yet

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    post a repo on githbu

  • abraabra ADMember ✭✭✭

    Is it also possible to force the scrolling of the ListView to the SelectedITem ? If I scroll down the ListView and insert new items at the beginning of the list, the selected item is set correctly to the first element, but the ListView does not scroll to it.

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    There should be a ScrollTo method in ListView
  • Amar_BaitAmar_Bait DZMember ✭✭✭✭✭
    edited June 2017

    Ya si @YoussefRafrafi warilna wach dert bech au moins na3arfou win rah le probleme.

  • abraabra ADMember ✭✭✭

    @AlessandroCaliaro- Thank you for the information. How can I call "ScrollTo" from the view-model in a MVVM application ?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I think you have not to call it in a ViewModel. ViewModel should not know about your UI. You should use it in your xaml.cs file. I think you have a Command in your viewModel that add the Item in the List. After you add the Item you can use a MessagingCenter.Send from ViewModel to cs. When xaml.cs receive the Message, here you can do List.ScrollTo

  • OfaimOfaim Member ✭✭

    The method that worked for me (regarding the question on the title) was neither one of the 3 earlier suggested, but:
    Mylist.SelectedItem = (Mylist.ItemsSource as List<MyViewModel>)[0];
    where MyViewModel is the type of the items using as the source for Mylist, which is the x:Name of the ListView in the xaml.

  • OfaimOfaim Member ✭✭

    The method that worked for me (regarding the question on the title) was neither one of the 3 earlier suggested, but:
    Mylist.SelectedItem = (Mylist.ItemsSource as List<MyViewModel>)[0];
    where MyViewModel is the type of the items using as the source for Mylist, which is the x:Name of the ListView in the xaml.

  • Marcel_NaritaMarcel_Narita Member ✭✭
    edited March 2018
    public ListView ListView { get { return listView; } }
            public MasterPage()
            {
                InitializeComponent();             
            }
            void Handle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
            {
                var p = e.Item as Project.Model.MenuItem;          
                if (p.Title=="whatYouWant") 
                {
                    ListView.SelectedItem = e.Item;
                }
            }
    

    or you can take a public variable like this:

                        public ListView ListView { get { return listView; } }//listView is the name from xaml:<ListView x:Name="listView"
                            public int ct = 0;
    
                            public MasterPage()
                            {
                                InitializeComponent();             
                            }
    
                            void Handle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
                            {
                                if(ct==0)
                                {
                                   ListView.SelectedItem = e.Item;
                            ct++;
    
                                }
                         }
    
  • JohnHadzimaJohnHadzima USMember ✭✭

    @Ofaim Where did you put the code that worked for you?

Sign In or Register to comment.