Loop through Controls in ScrollView and Get the Data

Jassim Al Rahma 1,616 Reputation points
2023-01-20T21:11:41.77+00:00

Hi,

In below XAML:

<ScrollView x:Name="ScrollViewDetails" Grid.Row="1">
    <VerticalStackLayout>
        <VerticalStackLayout x:Name="StackLayoutPhones">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Grid ColumnDefinitions="Auto,*">
                        <CheckBox ClassId="{Binding PhoneNumber}" Grid.Column="0" Margin="5" IsChecked="True" VerticalOptions="Center" />
                        <Label Grid.Column="1" Text="{Binding PhoneNumber}" VerticalOptions="Center" />
                    </Grid>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </VerticalStackLayout>
        <VerticalStackLayout x:Name="StackLayoutEmails">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <Grid ColumnDefinitions="Auto,*">
                        <CheckBox Grid.Column="0" Margin="5" IsChecked="True" VerticalOptions="Center" />
                        <Label Grid.Column="1" Text="{Binding EmailAddress}" VerticalOptions="Center" />
                    </Grid>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </VerticalStackLayout>
    </VerticalStackLayout>
</ScrollView>

How can I loop through all cntrols in the ScrollView and get the CheckBox's ClassId and the Label's Text for the Label next to the CheckBox so for example the result will be like this:

CheckBox Checked = True CheckBox Classid = Email Label Text = "******@email.com"

CheckBox Checked = False CheckBox Classid = Phone Label Text = "+1-222-333-4455"

Thanks,

Jassim

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-01-23T04:22:47.9433333+00:00

    Hello,

    =============Update==================

    You cannot put the if (ChildGirdListItem.GetType() == typeof(Label)) in the if (ChildGirdListItem.GetType() == typeof(CheckBox)), I move it to the outside of if (ChildGirdListItem.GetType() == typeof(CheckBox))

    Please note: I do not know about vcard SO I move it to foreach and create two local property  string id="", text=""; for testing.

    void ButtonGenerateQR_Clicked(System.Object sender, System.EventArgs e)
        {
            VerticalStackLayout view = ScrollViewDetails.Content as VerticalStackLayout;        var VScrollChilds = view.Children;
            vcard = new StringBuilder();
            vcard.AppendLine("BEGIN:VCARD");
            vcard.AppendLine("VERSION:3.0");
            vcard.AppendLine("N;CHARSET=utf-8:" + string.Format("{0};{1}", last_name, first_name));
            vcard.AppendLine("FN;CHARSET=utf-8:" + string.Format("{0} {1} {2} {3} {4}", prefix, first_name, middle_name, last_name, suffix));
            string id="", text="";
            foreach (var item in VScrollChilds)
            {
                VerticalStackLayout childVerticalStacklayout = item as VerticalStackLayout;
                if (childVerticalStacklayout != null)
                {
                    var it = childVerticalStacklayout.Children;
                    foreach (var ChildVStacklayoutItem in it)
                    {
                        Grid ChildVSLGrid = ChildVStacklayoutItem as Grid;
                        var ChildGirdList = ChildVSLGrid.Children;
                        if (ChildGirdList != null)
                        {
                            foreach (var ChildGirdListItem in ChildGirdList)
                            {
                                if (ChildGirdListItem.GetType() == typeof(CheckBox))
                                {
                                    CheckBox childCheckBox = ChildGirdListItem as CheckBox;
                                    if (childCheckBox.IsChecked == true)
                                    {
                                        
                              Console.WriteLine("=========" + childCheckBox.ClassId + "=============");
                                         id = childCheckBox.ClassId;
                                                                       
                                    }
                                }
                                if (ChildGirdListItem.GetType() == typeof(Label))
                                {
                                    Label childLabel = ChildGirdListItem as Label;
                                    Console.WriteLine("=========" + childLabel.Text + "=============");
                                                                    
     text = childLabel.Text;
                                }
                                vcard.AppendLine(string.Format("{0};PREF;WORK:{1}", id, text));
                            
                            }
                        }
                    }
                }
            }
            vcard.AppendLine("END:VCARD");
            Clipboard.SetTextAsync(vcard.ToString());
            this.ShowPopup(new ContactQR(vcard.ToString()));
        }
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.