How to force a ListView to display in the same row two diffrent data from differnt elements of an ObservableCollection

MOHAMED AMINE KESSENTINI 1 Reputation point
2020-12-08T09:29:02.497+00:00

Hello,

In my xaml file, I have a this ListView :

<ListView x:Name="MyListView" ItemsSource="{Binding MyData}" HasUnevenRows="true">  
            <ListView.ItemTemplate>  
                <DataTemplate x:Name="dataTemplate">  
                    <ViewCell>  
                        <FramePadding="2" BorderColor="DarkSlateGray">  
                            <Grid x:Name="grid" Padding="0">  
                                <Grid.ColumnDefinitions>  
                                    <ColumnDefinition Width="1.6*"/>  
                                    <ColumnDefinition Width="2.5*"/>  
                                    <ColumnDefinition Width="2.5*"/>  
                                    <ColumnDefinition Width="2.5*"/>  
                                    <ColumnDefinition Width="2.5*"/>  
                                    <ColumnDefinition Width="2.5*"/>  
                                    <ColumnDefinition Width="2.5*"/>  
                                </Grid.ColumnDefinitions>  

                                <Label Grid.Column="0" VerticalTextAlignment="Center" >  
                                    <Label.FormattedText>  
                                        <FormattedString>  
                                            <Span TextDecorations="Underline" TextColor="Black"  Text="{Binding dateName}"/>  
                                            <Span Text="{x:Static system:Environment.NewLine}"/>  
                                            <Span Text="{Binding date}"/>  
                                        </FormattedString>  
                                    </Label.FormattedText>  
                                </Label>  
                                <Label Grid.Column="{Binding gridNumber}" Grid.ColumnSpan="2" HorizontalTextAlignment="Center" BackgroundColor="{Binding BgColor}">  
                                    <Label.FormattedText>  
                                        <FormattedString>  
                                            <Span TextColor="Black"  Text="{Binding Information}"/>  
                                        </FormattedString>  
                                    </Label.FormattedText>  
                                </Label>  

                                <BoxView BackgroundColor="DarkSlateGray" WidthRequest="3" Grid.Column="0" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand" />  
                                <BoxView IsVisible="{Binding IsSimpleSeance}" BackgroundColor="DarkSlateGray" WidthRequest="1" Grid.Column="{Binding gridNumber}" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"/>  
                                <BoxView BackgroundColor="DarkSlateGray" WidthRequest="1" Grid.Column="2" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"/>  
                                <BoxView BackgroundColor="DarkSlateGray" WidthRequest="1" Grid.Column="3" HorizontalOptions="EndAndExpand" />  
                                <BoxView BackgroundColor="DarkSlateGray" WidthRequest="1" Grid.Column="5" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"/>  
                                <BoxView BackgroundColor="DarkSlateGray" WidthRequest="1" Grid.Column="6" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"/>  
                            </Grid>  
                        </Frame>  
                    </ViewCell>  
                </DataTemplate>  
            </ListView.ItemTemplate>  
        </ListView>  

And in my ViewModel file, I had this ObservableCollection<MyObject>:

lObservableCollectionist<MyObject> MyList = [  
 {"Monday", "07/12", "Some Data in First Line", 1},  
 { "Monday", "07/12", "Some Other Data ALSO in First Line",4},  
 { "Tuesday", "08/12", "New Data in Second Line" ,1},  
 { "Tuesday", "08/12", "New Other Data in Second Line",4 },  
 { "Tuesday", "08/12", "Newest Data Also in Second Line",6 },  
  ...  
  ]  

So, the problem is the ListView display each element of the ObservableCollection in new line like this 46154-listview-prob1.png

But what I want is to group the same day data in one line. Something like this

46178-listview-sol.png

So , Any Help !

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alessandro Caliaro 4,181 Reputation points
    2020-12-08T11:35:55.383+00:00

    I think you have to use grouped listview https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/userinterface-listview-grouping/

    or you have to modify your "Model" so you have one line for each date