Set ItemSource to List of Object, not string

robiharidrobiharid Member ✭✭

I want to set my ItemSource property of a ListView to the object Employee.

Employee has a Name and an ID field.

I want to display the name when viewing the list and once an item has been selected, grab the ID of that employee (or the Employee object).

I understand that by default ItemSource uses toString() - how can I override the ItemSource toString() to return the object and not the string?

Best Answer

  • LandLuLandLu Xamurai
    Accepted Answer

    @robiharid I understand that by default ItemSource uses toString() - how can I override the ItemSource toString() to return the object and not the string?

    However, we usually set the list view's items source to an object list. Each item of the list view is bound to the object of the list so that we could use binding to furtherly bind the controls aligned in the item of the list view.
    i.e. setting the items source like:

    public MainPage()
    {
        InitializeComponent();
    
        var list = new List<Employee>();
        for (int i=0; i<10; i++)
        {
            list.Add(new Employee { ID = "id" + i, Name = "name" + i });
        }
        listView.ItemsSource = list;
    }
    

    And the list view looks like:

    <ListView x:Name="listView" HasUnevenRows="True" ItemSelected="ListView_ItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding ID}"/>
                        <Entry Text="{Binding Name}"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    Retrieve the object through the selected event:

    private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Employee employee = e.SelectedItem as Employee;
    }
    

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited August 13

    Use the ItemTemplate property to define your own cells.

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/customizing-cell-appearance

    Or if you're fine with the default appearance and just need to know how to get the Employee object when the user taps a cell, it's as simple as doing a typecast. The ItemSelected and ItemTapped events and the ListView.SelectedItem property all return the object from your ItemsSource, not just the string representation.

  • LandLuLandLu Member, Xamarin Team Xamurai
    Accepted Answer

    @robiharid I understand that by default ItemSource uses toString() - how can I override the ItemSource toString() to return the object and not the string?

    However, we usually set the list view's items source to an object list. Each item of the list view is bound to the object of the list so that we could use binding to furtherly bind the controls aligned in the item of the list view.
    i.e. setting the items source like:

    public MainPage()
    {
        InitializeComponent();
    
        var list = new List<Employee>();
        for (int i=0; i<10; i++)
        {
            list.Add(new Employee { ID = "id" + i, Name = "name" + i });
        }
        listView.ItemsSource = list;
    }
    

    And the list view looks like:

    <ListView x:Name="listView" HasUnevenRows="True" ItemSelected="ListView_ItemSelected">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout>
                        <Label Text="{Binding ID}"/>
                        <Entry Text="{Binding Name}"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    Retrieve the object through the selected event:

    private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
    {
        Employee employee = e.SelectedItem as Employee;
    }
    
Sign In or Register to comment.