My listview does not bind correctly, I'm getting blank lines. ( I'm missing something in the code)
(in debug) the for each loop show there is data in the Items object but it does not show up in the listview.
Xaml
<?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="MapleSugarMaui.Views.CheckListPage"
xmlns:local="clr-namespace:MapleSugarMaui"
xmlns:vm="clr-namespace:MapleSugarMaui.ViewModels"
x:DataType="vm:CheckListViewModel"
xmlns:models="clr-namespace:MapleSugarMaui.Models"
Shell.NavBarIsVisible="false" >
<ContentPage.BindingContext>
<vm:CheckListViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
<StackLayout>
...
</StackLayout>
<!-- list view data -->
<ListView x:Name="ChecklistView"
ItemsSource="{Binding WorkCheckListItems}"
IsGroupingEnabled="True"
HasUnevenRows="True">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label FontSize="18" FontAttributes="Bold" BackgroundColor="White" Text="{Binding Sector}" />
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate >
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding BindingContext.OnEditCommand, Source={x:Reference ChecklistView}}"
CommandParameter="{Binding .}" />
</StackLayout.GestureRecognizers>
<Label Text="{Binding Sector}"
HorizontalOptions="Center"
VerticalOptions="Center"
TextColor="Black"
FontAttributes="Bold"
WidthRequest="53"/>
<BoxView WidthRequest="1" BackgroundColor="Black" HorizontalOptions="Start"/>
<Label Text="{Binding TreeNumber}"
HorizontalOptions="Center"
VerticalOptions="Center"
TextColor="Black"
FontAttributes="Bold"
xaml.cs
using MapleSugarMaui.ViewModels;
namespace MapleSugarMaui.Views;
public partial class CheckListPage : ContentPage
{
public CheckListPage()
{
InitializeComponent();
BindingContext = new CheckListViewModel();
}
}
Model.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MapleSugarMaui.Models;
using MapleSugarMaui.Services;
using System.Collections.ObjectModel;
namespace MapleSugarMaui.ViewModels
{
internal partial class CheckListViewModel : BaseViewModel
{
[ObservableProperty]
private ObservableCollection<WorkCheckListItem> workCheckListItems = new();
[ObservableProperty]
private WorkCheckListItem workCheckListItem = new();
[ObservableProperty]
private int groupSector;
[ObservableProperty]
public int sector;
[ObservableProperty]
public int treeNumber;
[ObservableProperty]
public int subTreeNumber;
...
public CheckListViewModel BindingContext { get; private set; }
/* ----------------------------------------------- main code ------------------------------------------- */
public CheckListViewModel()
{
StatusMsg = "Ready";
}
[RelayCommand]
private async Task LoadCheckListAction()
{
StatusMsg = " Checking for file ";
try
{
if (!FileSelected)
{
var CheckListFileName = await FilePicker.PickAsync();
InputFileName = CheckListFileName.FullPath;
}
if (InputFileName != null)
{
FileSelected = true;
// read JSON
var items = CheckListService.GetTrees(File.ReadAllText(InputFileName));
foreach (var item in items)
{
WorkCheckListItems.Add(item);
}
StatusMsg = " Ready ";
}
}
catch (Exception ex)
{
Console.WriteLine("iOS Main Exception: {0}", ex);
}
}
}
}