I am using Microsoft Visual Studio Community 2022(64-bit) Version 17.12.3. I am also using CommunityToolkit.Mvvm. I have CasesPage.xaml, CasesPage.xaml.cs and CasesPageModel with ReportedCasesView.cs which is the Model file. I am trying to display a query result in a CollectionView but the data does not show up although the number of records retrieved is indicated. Below are the files and code I tried to achieve the results.
CasesPage.xaml file:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bmcl.GhanaLawReport.Pages.CasesPage"
xmlns:local="clr-namespace:Bmcl.GhanaLawReport.Services"
xmlns:pagemodel="clr-namespace:Bmcl.GhanaLawReport.PageModels"
x:DataType="pagemodel:CasesPageModel"
xmlns:sfradiobutton="clr-namespace:Syncfusion.Maui.Buttons;assembly=Syncfusion.Maui.Buttons"
xmlns:inputLayout="clr-namespace:Syncfusion.Maui.Core;assembly=Syncfusion.Maui.Core"
xmlns:editors="clr-namespace:Syncfusion.Maui.Inputs;assembly=Syncfusion.Maui.Inputs"
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.ListView;assembly=Syncfusion.Maui.ListView"
Title="Reported Cases">
<ContentPage.BindingContext>
<pagemodel:CasesPageModel />
</ContentPage.BindingContext>
<ScrollView>
<VerticalStackLayout>
<Label Margin="10,0,10,0"
Text="Ghana Law Reports"
VerticalOptions="Center"
HorizontalOptions="Center" FontSize="Title" />
<Image Source="ghanacoatofarms.png" HeightRequest="50" Aspect="AspectFit" />
<StackLayout Margin="5">
<sfradiobutton:SfRadioGroup x:Name="CasesGroup">
<sfradiobutton:SfRadioButton
x:Name="ReportYear"
Text="Case By Report Year"
CheckedColor="Red"
UncheckedColor="Black"
StateChanged="ReportYear_StateChanged"/>
<!--Setting ItemsSource-->
<editors:SfComboBox x:Name="LawReportComboBox"
WidthRequest="250"
HeightRequest = "50"
IsEnabled="False"
DisplayMemberPath="ReportName"
TextMemberPath="ReportName"
ItemsSource="{Binding LawReportList}"
SelectionChanged="LawReportComboBox_SelectionChanged"/>
<sfradiobutton:SfRadioButton
x:Name="AnyTextCaseTitle"
Text="Case Title By Any Text"
CheckedColor="Red"
UncheckedColor="Black"
StateChanged="AnyTextCaseTitle_StateChanged"/>
<Entry x:Name="CaseTitleEntry"
IsEnabled="False"
TextChanged="CaseTitleEntry_TextChanged"
Margin="30,0,10,0"/>
</sfradiobutton:SfRadioGroup>
</StackLayout>
<StackLayout Margin="10">
<Button x:Name="Search" Text="Search"
BackgroundColor="Red"
TextColor="White"
Command="{Binding SearchCommand}"/>
</StackLayout>
<VerticalStackLayout Margin="0,10,0,10">
<Label Text="{Binding RecordsReturned}"
TextColor="Red" FontSize="13"
FontAttributes="Bold"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Center"/>
</VerticalStackLayout>
<!--ListView Area-->
<CollectionView x:Name="CasesCollectionView"
SelectionMode="Single"
EmptyView="No Data"
ItemsLayout="VerticalGrid"
ItemsSource="{Binding CaseTitlesList}">
<CollectionView.ItemTemplate>
<DataTemplate>
<VerticalStackLayout Padding="5" Margin="10" VerticalOptions="Center">
<Label Text="{Binding CaseName}" FontSize="Medium"/>
<Label Text="{Binding CourtName}" FontSize="Small"/>
<Label Text="{Binding ReportName}" FontSize="Small"/>
</VerticalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
CasesPage.xaml.cs file
using Bmcl.GhanaLawReport.Models;
using Bmcl.GhanaLawReport.PageModels;
using Bmcl.GhanaLawReport.Services;
namespace Bmcl.GhanaLawReport.Pages;
public partial class CasesPage : ContentPage
{
public CasesPage()
{
InitializeComponent();
}
private void ReportYear_StateChanged(object sender, Syncfusion.Maui.Buttons.StateChangedEventArgs e)
{
if(e.IsChecked.HasValue && e.IsChecked.Value)
{
AppProperties.ReportedCasesSearchOption = 1;
LawReportComboBox.IsEnabled = true;
}
else
{
LawReportComboBox.IsEnabled = false;
}
}
private void AnyTextCaseTitle_StateChanged(object sender, Syncfusion.Maui.Buttons.StateChangedEventArgs e)
{
if(e.IsChecked.HasValue && e.IsChecked.Value)
{
AppProperties.ReportedCasesSearchOption = 2;
CaseTitleEntry.IsEnabled = true;
}
else
{
CaseTitleEntry.IsEnabled = false;
}
}
private void CaseTitleEntry_TextChanged(object sender, TextChangedEventArgs e)
{
AppProperties.ReportedCasesAnyTextParameter = CaseTitleEntry.Text;
}
private void LawReportComboBox_SelectionChanged(object sender, Syncfusion.Maui.Inputs.SelectionChangedEventArgs e)
{
AppProperties.LawReportComboParameter = LawReportComboBox.Text;
}
}
CasesPageModel.cs file (ViewModel if you like)
using Bmcl.GhanaLawReport.Models;
using Bmcl.GhanaLawReport.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;
namespace Bmcl.GhanaLawReport.PageModels
{
public partial class CasesPageModel : ObservableObject
{
private Int32 totalRecords = 0;
[ObservableProperty]
string recordsReturned = "";
[ObservableProperty]
ObservableCollection<ReportedCasesView> caseTitlesList = [];
[ObservableProperty]
ObservableCollection<LawReportIndexView> lawReportList = [];
[ObservableProperty]
int indexId = 0;
[ObservableProperty]
int caseId = 0;
[ObservableProperty]
int courtId = 0;
[ObservableProperty]
int reportId = 0;
[ObservableProperty]
string? caseName;
[ObservableProperty]
string? courtName;
[ObservableProperty]
string? reportName;
[ObservableProperty]
string? judges;
public CasesPageModel()
{
GetLawReports();
}
private void GetLawReports()
{
var lawReports = Data.LawReport.ReadAllLawReports.Read();
if (lawReports != null)
LawReportList = new ObservableCollection<LawReportIndexView>(lawReports);
}
[RelayCommand]
public async Task Search()
{
await GetSerchOptions();
}
private async Task GetSerchOptions()
{
switch(AppProperties.ReportedCasesSearchOption)
{
case 1:
if(AppProperties.LawReportComboParameter == string.Empty ||
AppProperties.LawReportComboParameter == null)
{
//--Message here
if(Application.Current?.Windows.Count > 0)
{
var page = Application.Current.Windows[0].Page;
if(page != null)
{
await page.DisplayAlert("Reported Case Error",
"Please select a Law Report ", "OK");
}
}
return;
}
var caseTitles = await Data.ReportedCase.ReadCaseTitleByReport.ReadAsync(AppProperties.LawReportComboParameter);
if (caseTitles != null)
{
CaseTitlesList = new ObservableCollection<ReportedCasesView>(caseTitles);
totalRecords = caseTitles.Count;
RecordsReturned = $"Showing {totalRecords} Returned Records";
}
break;
case 2:
if(AppProperties.ReportedCasesAnyTextParameter == string.Empty ||
AppProperties.ReportedCasesAnyTextParameter == null)
{
//--Message here
if(Application.Current?.Windows.Count > 0)
{
var page = Application.Current.Windows[0].Page;
if(page != null)
{
await page.DisplayAlert("Reported Case Error",
"Please supply a Case Title ", "OK");
}
}
return;
}
var cases = await Data.ReportedCase.ReadCaseTitleByAnyText.ReadAsync(AppProperties.ReportedCasesAnyTextParameter);
if (cases != null)
{
CaseTitlesList = new ObservableCollection<ReportedCasesView>(cases);
totalRecords = cases.Count;
RecordsReturned = $"Showing {totalRecords} Returned Records";
}
break;
}
}
}
}
ReportedCasesView (Model file)
namespace Bmcl.GhanaLawReport.Models
{
public class ReportedCasesView
{
public int IndexId { get; set; }
public int CaseId { get; set; }
public int CourtId { get; set; }
public int ReportId { get; set; }
public string? CaseName { get; set; }
public string? CourtName { get; set; }
public string? ReportName { get; set; }
public string? Judges { get; set; }
}
}
What I want to achieve is very simple. For the queried data’s result to be displayed the CollectionView.
Thank you in advance for your help.
4