Problem with DataTemplateSelector
I have a Listview which is populated from a table in sqlite as follows:
var ts = await _dbService.GetTimesheetLines();
ListView.BindingContext = ts;
ListView.ItemsSource = ts;
ListView.ItemTemplate = new StatusType();
The _dbservice calls the following code:
public async Task<List<Timesheet>> GetTimesheetLines()
{
return await _connection.Table<Timesheet>().ToListAsync();
}
underneath the item source line I have a call to the DataTemplateSelector:
ListView.ItemTemplate = new StatusType();
... and this class is written as follows:
public class StatusType : DataTemplateSelector
{
public DataTemplate enteredTemplate { get; set; }
public DataTemplate receivedTemplate { get; set; }
public DataTemplate approvedTemplate { get; set; }
public StatusType()
{
enteredTemplate = new DataTemplate(typeof(List<>));
receivedTemplate = new DataTemplate(typeof(List<>));
approvedTemplate = new DataTemplate(typeof(List<>));
}
protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
{
var ts = item as List; // not correct
if (ts.entryNo == null) return enteredTemplate;
if (ts.entryNo != null && ts.approved == true) return approvedTemplate;
else return receivedTemplate;
}
}
How can I reference the list "ts" in the separate StatusType class?
Any help would be most appreciated.
Regards
Ross
Hi Ross Bell Syer,
We would like to inform you that when binding an IEnumerable collection from the ViewModel to the ListView.ItemSource, the each item/data from that collection, is set as the BindingContext for each ListViewItem (ItemTemplate).
For example, let's say ,Model name is "EmployeeInfo" and binding List<EmployeeInfo> as ItemsSource of ListView. In this case, instance of EmployeeInfo will be set as BindingContext of respective item.
So, in the OnSelectTemplate method, the "item" parameter only provides the binding context (Model object) of respective item. If you need to access the entire list, you can get the SfListView instance from "container" parameter and use its ItemsSource property to access the items for your customization.
|
protected override DataTemplate OnSelectTemplate(object item, BindableObject container) { var ts = (container as SfListView);
if (ts != null && ts.ItemsSource is IEnumerable<Models> items) { itemList = items.ToList(); }
} |
Regards,
KamalaKannan S.
Hi Kamala,
Sorry for the late reply. Thank you very much for the information above. In the end the process was a bit simpler. I just needed to enter some more information on the xaml form.
I do have another question though.
When using your dropdown box in search mode i.e. start typing text to see the list of filtered results - how do you prevent the user from typing in whatever they want as there doesn't seem to be a control to ensure they actually select an item in the list?
thanks
Ross
Hi Ross Bell Syer,
As you mentioned in the last update, we need some additional details about the query. Could you please share the following information:
1. Which control are you using?
2. Provide more details about how you would like to customize the sample, including screenshots or screen recordings if possible.
3. If available, could you please share the sample?
Regards,
KamalaKannan S.
- 3 Replies
- 2 Participants
-
RB Ross Bell Syer
- Aug 7, 2024 03:33 AM UTC
- Aug 16, 2024 03:47 PM UTC