c# – How to retrieve a field in .NET 8 Maui CollectionView and pass it to another page


Below is a screenshot of my collection view containing a returned query of CaseName, ReportName, Court and Judges. There are two buttons (Index and Judgment). When the user taps or click any one of the buttons, for instance when the use taps on the Index button of the first cell or view cell, then I will be able to pick the INKUMSAH V. THE REPUBLIC field and send to the next page as a parameter.

The display alert was to show me if the case name has been retrieved. Just making sure.

I will be most glad if anyone of you out there can give me a helping hand. Thank you very much indeed, in advance.

CollectionView with a query results

        <CollectionView x:Name="CasesCollectionView" 
                    ItemsSource="{Binding ReportedCasesList}" 
                    Grid.Row="2">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <Frame x:Name="frame" 
                           CornerRadius="10" 
                           Margin="10,0,10,10" 
                           HasShadow="True">
                        <StackLayout>
                            <Label x:Name="Case" 
                                    TextColor="Red" 
                                    Text="{Binding Source={x:Reference Case}, Path=BindingContext.CaseName}"
                                    FontSize="13" 
                                    FontAttributes="Bold" 
                                   Padding="0,0,0,10" />
                                
                            <Label x:Name="Report" 
                                    TextColor="Black" 
                                    FontSize="12" 
                                    Text="{Binding ReportName}" />
                                
                            <Label x:Name="Court" 
                                    TextColor="Black" 
                                    FontSize="12" 
                                    Text="{Binding CourtName}" />
                                
                            <Label x:Name="Judge" 
                                    TextColor="Black" 
                                    FontSize="12" 
                                    Text="{Binding Judges}" 
                                    HorizontalTextAlignment="End" 
                                    FontAttributes="Italic" 
                                    Padding="0,10,0,0" Margin="0,0,0,10" />
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Button x:Name="IndexButton" 
                                        TextColor="White" 
                                        BackgroundColor="Red"
                                        FontSize="12" 
                                        Text="Index" 
                                        Grid.Column="0" Margin="5,0,5,0" 
                                        Command="{Binding Source={x:Reference CasesCollectionView}, Path=BindingContext.GoToCaseIndexCommand}"/>
                                <Button x:Name="JudgmentButton" 
                                        TextColor="White" 
                                        BackgroundColor="Red"
                                        FontSize="12" 
                                        Text="Judgment" 
                                        Grid.Column="1" Margin="5,0,5,0" 
                                        Command="{Binding Source={x:Reference CasesCollectionView}, Path=BindingContext.GoToCaseJudgmentCommand}"/>
                            </Grid>
                        </StackLayout>
                    </Frame> 

                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>



    [ObservableProperty]
    public string? caseName;


    [RelayCommand]
    public async Task GoToCaseIndex()
    {
        if (Application.Current?.MainPage != null)
            await Application.Current.MainPage.DisplayAlert("Cases",
                CaseName, "OK");

        await Shell.Current.GoToAsync(nameof(AreasOfLawResultsPage));
    }

    
    [RelayCommand]
    public async Task GoToCaseJudgment()
    {
        if (Application.Current?.MainPage != null)
            await Application.Current.MainPage.DisplayAlert("Cases",
                CaseName, "OK");

        await Shell.Current.GoToAsync(nameof(AreasOfLawResultsPage));
    }

Leave a Reply

Your email address will not be published. Required fields are marked *