How can I change the BackgroundColor for the first ListView's Item automatically?

Marcel_NaritaMarcel_Narita Member ✭✭
edited March 2018 in Xamarin.Forms

How can I change the BackgroundColor for the first ListView's Item automatically? Because when I open the application, I'm on the first Item's Page. I want automatically selection for the first item.

Best Answers

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭
    Accepted Answer

    I think you can set SelectedItem property https://developer.xamarin.com/api/type/Xamarin.Forms.ListView/

  • Marcel_NaritaMarcel_Narita Member ✭✭
    Accepted Answer

    I found a solution. :smile:

    public int ct = 0;
    void Handle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
    {
    if (ct==0)
    {
    ListView.SelectedItem = e.Item;
    ct++;
    }
    }

  • Marcel_NaritaMarcel_Narita Member ✭✭

    Or like this, if you have some preferences:

    * void Handle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
            {
                var p = e.Item as Project.Model.MenuItem;
                if (p.Title=="SCHEDULES")//What I want
                {
                    ListView.SelectedItem = e.Item;
    
                }
            }*
    

    Or if you want to select the last item from listview:

            * public ListView ListView { get { return listView; } }//listView is the name from xaml:<ListView x:Name="listView"
                public int ct = 0,ct1=0;
    
                public MasterPage()
                {
                    InitializeComponent();             
                }
    
                void Handle_ItemAppearing(object sender, Xamarin.Forms.ItemVisibilityEventArgs e)
                {
                    var p = e.Item as Project.Model.MenuItem;
                    ListView list = (ListView)sender;
    
                    if(ct==0)
                    {
                        foreach (var v in list.ItemsSource)
                        {
                            ct++;
                        }
                    }
                    ct1++;
    
                    if(ct1==ct)
                    {
                        ListView.SelectedItem = e.Item;
                    }}* 
    

    if someone has another method, please tell us.

Sign In or Register to comment.