I'm still fairly new to xaml and .net maui but I don't really know what search terms to even use. I want to list today's task notifications in xaml with a collection view. I want this list, when displaying in xaml collection view, to pull details from another list using the TaskID as an identifier key.
For example, roughly:
class Notification:
int Id;
int TaskID;
int Title;
DateTime NotificationTime;
class Task:
int Id;
string Name;
string Details;
UpcomingNotificationsViewModel:
List<Notification> NotificationsList { get; set; }
List<Task> TasksList { get; set; }
async void LoadData()
{
var result = from s in NotificationsList
where s.NotificationTime.Date == today
group s by s.NotificationTime into g
select new GroupedNotification{ NotificationTime = g.Key, Notification = g.ToList() };
GroupedNotifications = result;
}
public class GroupedNotification
{
public DateTime NotifyTime { get; set; }
public List<Notification> Notification { get; set; }
}
In the XAML, while iterating over CollectionView of Notifications, I want to also pull data from the TasksList like the Task.Name, Task.Details, and so on.
<CollectionView ItemsSource="{Binding GroupedNotifications}">
<CollectionView.ItemTemplate>
<DataTemplate>
<VerticalStackLayout>
<!-- Display the group header (time) -->
<Label Text="{Binding NotifyTime, StringFormat='{}{0:h\\:mm tt}'}"
FontAttributes="Bold"
TextColor="Blue"/>
<!-- Display the notifications within the group -->
<CollectionView ItemsSource="{Binding Notification}">
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="0,5">
<Frame Padding="10,10">
<StackLayout>
<Label Text="{Binding Title}" />
<Label Text="{Binding NotificationTime}" />
//this label below, how can I link it to TaskList and get details based on TaskID from this Notification List?
<Label Text="{Binding How can I link this to show Task details based on the ID??}" />
<!-- Other properties... -->
</StackLayout>
</Frame>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>`
I tried several different iterations of this code, I tried searching for the past three hours, tried asking ChatGPT and Gemini but I cannot find what I'm looking for. I need someone to point me in the correct direction.
As I understand it, you are displaying some daily tasks in a collection view, and want to query the database for task details based on some event (for example, tapping the task line). There are many ways to do this, but I hope to "point you in the right direction" as you said. Two things to know in advance: For SQLite I'm specifically using the sqlite-pcl-net NuGet for this sample. Also, to try and avoid this answer going even longer, I put the full code on GitHub to browse or clone.
In this version, there are two tables in the SQLite database, corresponding to a
TaskItemrecord class for the parent item and aDetailItemrecord class for its subitems. For the task item, the data template that will host it in the xaml will attach aTapGestureRecognizerto the label displaying the task description, which will call theLabelTappedCommandin the view model. In that method, the database will be queried for detail items with aParentIdvalue equal to theTaskItemthat has been tapped. Once the detail items are retrieved, one approach would be to use shell navigation to display the details in an entirely different view, or alternatively stay on the main page and useOnePagenavigation to hide the task grid and show the details grid as is the case here.The
DetailItemis expecting to be hosted in a data template that has a checkbox to indicate whether the task has been completed, and when the user changes this value it will be committed to the database. A word of caution would be that having theDoneproperty directly bound to the database update could be problematic when the property is changing as a result of a query that is loading it. In fact, the database is likely to hang. (This kind of circularity is a common issue when loading objects with persisted properties.) To prevent this, this sample uses a reference-counted semaphore that is checked out before the query is made.Main Page
In the screenshot shown above, the top level collection view isn't showing
TaskItemorDetailItemobjects directly. For demonstration purposes, the main view consists of sevenCardobjects representing today, tomorrow, and the other five days in the week to come.The view can be refreshed by executing the
RefreshCommandin the main view model, and (for example) it is always invoked in response to theAppearing) event.The main page is one-time populated with a list of seven cards, and when refreshed this list is iterated. A query is made on each card for tasks with matching day, and these are added to the
TaskItemscollection of the card.The xaml features a responsive layout that varies the height of the cards depending on the number of tasks present in the day.
Xaml