list of absolute layout children

Shereif Awad 96 Reputation points
2021-03-10T09:49:38.52+00:00

I have muliple sheets with cutted boxes in specific positions in each sheet

I use code behind to display it

    private void paint(List<StockSheet> results)
    {
        // Create SKCanvasView to view result
        SheetsNumber = results.Count().ToString();
        //int row = 1;
        foreach (var sheet in results)
        {
            var absolute = new AbsoluteLayout();
            foreach (var panel in sheet.CuttedPanels)
            {
                if (panel.Position != null)
                {
                    var frame = new Frame
                    {
                        Padding = 0,
                        BorderColor = Color.Black,
                        BackgroundColor = Color.FromHex(GenerateColor()),
                        Content = new Label
                        {

                            TextColor = Color.White,
                            Text = $"{panel.Width} * {panel.Length} ID:{panel.Id}",
                            HorizontalTextAlignment = TextAlignment.Center,
                            VerticalTextAlignment = TextAlignment.Center
                        },



                    };
                    Rectangle rect = new Rectangle();
                    rect = new Rectangle(panel.Position.X * 1.2, panel.Position.Y * 1.2, panel.Width * 1.2, panel.Length * 1.2);

                    absolute.Children.Add(frame, rect);


                }
            }
            Items.Add(absolute);

        }

and in Xaml as follow

        <ListView ItemsSource="{Binding Items}"
                  HasUnevenRows="True"
                  SeparatorColor="Black">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ContentView Content="{Binding .}" BackgroundColor="White" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

but now I want to use all using xaml no code behind so i tried to do nested list

        <CollectionView ItemsSource="{Binding Items}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <AbsoluteLayout VerticalOptions="FillAndExpand">

                        <ListView x:Name="collection" ItemsSource="{Binding CuttedPanels}"
                                  VerticalOptions="FillAndExpand"
                                  HasUnevenRows="True">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>

                                        <Frame Padding="0" 
                                               BackgroundColor="{Binding Color, Converter={converter:StringToColorConverter}}"
                                               AbsoluteLayout.LayoutBounds="{Binding .,Converter={converter:BoundsToRectangleConverter}}"
                                               AbsoluteLayout.LayoutFlags="None"
                                       BorderColor="Black">
                                                <Label Text="{Binding Title}" TextColor="White"
                                           HorizontalTextAlignment="Center"
                                           VerticalTextAlignment="Center" />
                                            </Frame>

                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </AbsoluteLayout>

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

and the value converter to use for bounds

        if (value != null && value is Box box)
            return new Rectangle(box.Position.X * 1.2, box.Position.Y * 1.2, box.Width * 1.2, box.Length * 1.2);
        else
            return new Rectangle(0, 0, 0, 0);

but can not get the same behave as before

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

Accepted answer
  1. Shereif Awad 96 Reputation points
    2021-03-10T16:57:43.247+00:00
            <CollectionView x:Name="collection" ItemsSource="{Binding Items}" AbsoluteLayout.LayoutBounds="0,0,1,1"
                            AbsoluteLayout.LayoutFlags="All">
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <AbsoluteLayout BindableLayout.ItemsSource="{Binding CuttedPanels}"
                                      VerticalOptions="FillAndExpand">
                            <BindableLayout.ItemTemplate>
                                <DataTemplate>
    
                                    <Frame Padding="0" 
                                                   BackgroundColor="{Binding Color, Converter={converter:StringToColorConverter}}"
                                                   AbsoluteLayout.LayoutBounds="{Binding .,Converter={converter:BoundsToRectangleConverter}}"
                                                   AbsoluteLayout.LayoutFlags="None"
                                           BorderColor="Black">
                                        <Label Text="{Binding Title}" TextColor="White"
                                               HorizontalTextAlignment="Center"
                                               VerticalTextAlignment="Center" />
                                    </Frame>
    
                                </DataTemplate>
                            </BindableLayout.ItemTemplate>
                        </AbsoluteLayout>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.