Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to get the id of the toggled switch item in the listview?

SreeeeSreeee INMember ✭✭✭✭✭

I have a listview and all the listview item contain a switch at the right end like below picture.

When I select an item the switch fires the Toggled event. My codes are adding below:

Xaml:

     <Switch
           Toggled="OnToggledEvent"
           HorizontalOptions="EndAndExpand"
           VerticalOptions="CenterAndExpand"/>

Xaml.cs:

void OnToggledEvent(object sender, ToggledEventArgs args)
    {
        //How I can access the userId of the selected item
    }

Model class:

public class DirectoryResponse
    {
        public List<UserProfileHBList> userProfileHBList { get; set; }
    }

    public class UserProfileHBList
    {
        public UserProfileTO userProfileTO { get; set; }
    }

    public class UserProfileTO
    {
        public string userId { get; set; }
        public string firstName { get; set; }
        public string email { get; set; }
        public string lastName { get; set; }
    }

I need the userId of the selected item, how I can access that.
Thanks in advance :smile:

Best Answer

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited May 2018 Accepted Answer

    Got the id of the selected item by below code:

    In the OnToggledEvent(object sender, EventArgs args) we can get the Parent ViewCell depending on your hierarchy:

    public void OnToggledEvent(object sender, EventArgs args)
    {
        //Here I use a Grid to wrap the content so I need to use two Parent to find the ViewCell
        ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
    
        // If you set the list<UserProfileHBList> as the ListView's ItemsSource, we can find the model through BindingContext
        UserProfileHBList model = cell.BindingContext as UserProfileHBList;
    
        // Then the userId can be known
        if (model != null)
        {
            Debug.WriteLine("Userid:>>"+model.userProfileTO.userId);
        }
    
    }
    

Answers

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited May 2018

    Update:

    I tried like below, but getting an exception.

    public class MyToggledEventArgs : EventArgs
        {
            public UserProfileHBList MyItem { get; set; }
            public MyToggledEventArgs(UserProfileHBList item)
            {
                this.MyItem = item;
            }
        }
    
        void OnToggledEvent(object sender, MyToggledEventArgs args)
        {
            var item = args.MyItem;
            if (item != null)
            {
                Debug.WriteLine("Userid:>>"+item.userProfileTO.userId);
            }
        }
    
  • JamesLaveryJamesLavery GBBeta, University ✭✭✭✭✭

    And the exception is? 'An exception' doesn't help us help you, I'm afraid.

    Regarding your design, it would be better to keep to an MVVM pattern, and use the bindings of the Switch rather than the toggled Event. You'll probably need to use relative bindings to bind the Switch IsToggled property to Command in the associated PageModel/ViewModel.

  • SreeeeSreeee INMember ✭✭✭✭✭
    edited May 2018 Accepted Answer

    Got the id of the selected item by below code:

    In the OnToggledEvent(object sender, EventArgs args) we can get the Parent ViewCell depending on your hierarchy:

    public void OnToggledEvent(object sender, EventArgs args)
    {
        //Here I use a Grid to wrap the content so I need to use two Parent to find the ViewCell
        ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
    
        // If you set the list<UserProfileHBList> as the ListView's ItemsSource, we can find the model through BindingContext
        UserProfileHBList model = cell.BindingContext as UserProfileHBList;
    
        // Then the userId can be known
        if (model != null)
        {
            Debug.WriteLine("Userid:>>"+model.userProfileTO.userId);
        }
    
    }
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Since the sender is the switch that was toggled... And BindingContext is inherited...
    Wouldn't it be more direct to just get the BindingContext of the sender? That would be the actual element in the collection being used as the Source in the ListView

  • shubham1shubham1 Member ✭✭✭

    Hi @Sreeee ,
    What if I want to hide or show the switch(inside the listview) on button click which is outside from the listview?

  • Liêm_NguyễnLiêm_Nguyễn USMember ✭✭✭✭

    @Sreeee said:
    Got the id of the selected item by below code:

    In the OnToggledEvent(object sender, EventArgs args) we can get the Parent ViewCell depending on your hierarchy:

    public void OnToggledEvent(object sender, EventArgs args)
    {
    //Here I use a Grid to wrap the content so I need to use two Parent to find the ViewCell
    ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;

    // If you set the list as the ListView's ItemsSource, we can find the model through BindingContext
    UserProfileHBList model = cell.BindingContext as UserProfileHBList;

    // Then the userId can be known
    if (model != null)
    {
    Debug.WriteLine("Userid:>>"+model.userProfileTO.userId);
    }

    }

    Thank you!

Sign In or Register to comment.