I’m trying to build a table in .NET MAUI based on a Grid Layout. This is the code:
<CollectionView ItemsSource="{Binding digitalInputs}">
<CollectionView.Header>
<Grid ColumnDefinitions="*,*,*,*">
<Label Text="Name" Grid.Column="0"/>
<Label Text="Typ" Grid.Column="1"/>
<Label Text="Status" Grid.Column="2"/>
<Label Text="Aktiv" Grid.Column="3"/>
</Grid>
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="services:DigitalInput">
<Grid ColumnDefinitions="*,*,*,*">
<Label Text="{Binding pName}" Grid.Column="0"/>
<Label Text="{Binding pDigitalType}" Grid.Column="1"/>
<Label Text="{Binding pValueText}" Grid.Column="2"/>
<Label Text="{Binding pActive}" Grid.Column="3"/>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
This is the result when running in Debug Mode on MacCatalyst (Visual Studio for Mac):
Now I wonder how I can align the header grid properly to the grid in the data template? Does someone have a suggestion on how I can improve the code to build a proper table?
Edit: This seems to be a bug in the IDE. When I change the HorizontalOptions
property on the Grid in the CollectionView.Header
, as a comment suggested, the XAML Hot-Reload triggers a re-rendering of the view and all of a sudden the header grid aligns correctly with the grid in the ItemTemplate.
2
Answers
If you mean by arranging children horizontally first and then pushing down to the next row then the current MAUI still does not support that, you can only trigger span (also span isn’t changing at runtime on WinUI right now, I think the team is fixing it) or create a custom one
Usage
I tested the code you provided in iOS, Windows in
MAUI
. And it can align the header grid properly to the grid in the data template in CollectionView. So the issue could be related with theservices:DigitalInput
retrieving the data, they should be correctly formatted with no blank space in those properties.Below are the code sample and running output, hope it can shed some light for you!
XAML:
Code-behind:
iOS output:
Windows output:
Update:
This seems to be a potential issue in the IDE. When changing the
HorizontalOptions
property on the Grid as Jason suggested, the header grid aligns correctly with the grid in the ItemTemplate.