0

I am new to xamarin/Mobile development. I got a task to create a single listview which should populate the objects of two different classes using xamarin.forms.

`Class A
{
 string PendingRequestID;
 string PendingRequestStatus;
 string PendingRequest;
}

Class B
{
 String CompletedRequestId;
 String ApprovedByUsername;
 DateTime CompletedTime
}

` above are the two different entities and I need to populate the list of objects of both in single list view. Each object is having its own separate UI layout.

How can i specify multiple ItemSource in a ListView in xamarin.forms?

Please help me.

2 Answers 2

2

Just create a property of type ObservableCollection<object> for ItemsSource on ListView and use a data template selector in order to provide item-type based template(s) to ListView.

An example can be found here

EDIT - 1 : Sample code

class MyDataTemplateSelector : Xamarin.Forms.DataTemplateSelector
{
    public MyDataTemplateSelector()
    {
        // Retain instances!
        this._typeADataTemplate = new DataTemplate(typeof(TypeAViewCell));
        this._typeBDataTemplate = new DataTemplate(typeof(TypeBViewCell));
    }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        if (item == null)
            return null;
        return (item is A) ? this._typeADataTemplate : this._typeBDataTemplate;
    }

    private readonly DataTemplate _typeADataTemplate;
    private readonly DataTemplate _typeBDataTemplate;
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is the best approach, data templates are ideal for this scenario.
@Sharada Gururaj. Thank you for the solution. I will try with this solution now.
@sharada Gururaj. Its working fine :). How can I add event handler for a control resides in customview cell? Should I need to add it in customviewcell's cs page or in my Maincontentpage cs file? If I have to add it in Maincontentpage's cs file; how to specify it in Customviewcell's xamal?
It would be easier to use event to command behavior; or tap recognizer command; especially while working through templated controls
@Sharada Gururaj Thank you. Now I have added gesturerecognizer to my label and added one command parameter. But I am not able to get the command parameter in the code behind of viewcell. var itemSender = (Xamarin.Forms.View)sender; var itemTapGestureRecognizer = (TapGestureRecognizer)itemSender.GestureRecognizers[0];
|
1

You can't assign multiple types for ItemSource. Therefore, one way is to use a third class as a base class and then derive your A and B from it. Then use this C type as ItemSource.

Class C{
   ...
}

Class A : C
{
   string PendingRequestID;
   string PendingRequestStatus;
   string PendingRequest;
}

Class B : C
{
   String CompletedRequestId;
   String ApprovedByUsername;
   DateTime CompletedTime
}

Note that you will then need to check for the right subtype during runtime whenever you click on an Item in the list.

Hope it helps!

3 Comments

To add, when you define the ObservableCollection which you bind your listview to, it will be ObservableCollection<C> where C is the class your two sub types inherit from.
@Tait's Thanks for your valuable suggestion, but in my scenario I need to have a different kind of UI layout based on the type.
@Thavudu Ok, sorry I've missed that info. Then DataTemplateSelector is the way to go. Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.