Search code examples
xamarin.formsxamarin.android

How to retrieve child controls in Xamarin StackLayout


`

<StackLayout
                        x:Name="xGrpTabBtns"
                        Grid.Row="0"
                        HorizontalOptions="StartAndExpand"
                        Orientation="Horizontal"
                        Spacing="0">



                        <!--#region Tab Button Gerneral-->
                        <Grid ColumnSpacing="0" RowSpacing="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="1" />
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="1" />
                            </Grid.ColumnDefinitions>

                            <controls:CtrlButton
                                Grid.Row="0"
                                Grid.Column="0"
                                BorderColor="Transparent"
                                BorderRadius="0"
                                BorderWidth="0"
                                Clicked="BtnTabBtnClicked"
                                Style="{StaticResource GuoupStyle}"
                                Text="Gerneral"
                                WidthRequest="180" />

                            <!--#region Bottom-->
                            <BoxView
                                x:Name="xBtnGerneralBotBorder"
                                Grid.Row="1"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                BackgroundColor="{StaticResource ColorBorder000}" />
                            <!--  Endregion  -->


                            <!--#region Right-->
                            <BoxView
                                Grid.Row="0"
                                Grid.RowSpan="2"
                                Grid.Column="1"
                                BackgroundColor="{StaticResource ColorBorder000}" />
                            <!--  Endregion  -->

                        </Grid>
                        <!--#endregion-->



                        <!--#region Tab Button Information-->
                        <Grid ColumnSpacing="0" RowSpacing="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="1" />
                            </Grid.RowDefinitions>

                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="1" />
                            </Grid.ColumnDefinitions>

                            <controls:CtrlButton
                                Grid.Row="0"
                                Grid.Column="0"
                                BorderColor="Transparent"
                                BorderRadius="0"
                                BorderWidth="0"
                                Clicked="BtnTabBtnClicked"
                                Style="{StaticResource GuoupStyle}"
                                Text="Information"
                                WidthRequest="180" />

                            <!--#region Bottom-->
                            <BoxView
                                x:Name="xBtnInformBotBorder"
                                Grid.Row="1"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                BackgroundColor="{StaticResource ColorBorder000}" />
                            <!--  Endregion  -->


                            <!--#region Right-->
                            <BoxView
                                Grid.Row="0"
                                Grid.RowSpan="2"
                                Grid.Column="1"
                                BackgroundColor="{StaticResource ColorBorder000}" />
                            <!--  Endregion  -->

                        </Grid>
                        <!--#endregion-->
                    </StackLayout>

`

I want to retrieve a CtrlButton from the grid.

var buttons = this.xGrpTabBtns.Children.Where(x => x is CtrlButton); I used it like this, but I couldn't get the button to work. Is there a way to get all the CtrlButtons inside a stack layout?

var buttons = this.xGrpTabBtns.Children.Where(x => x is CtrlButton);

I tried this, but it was null.

The effect I was expecting is to get all the CtrlButtons inside the Stacklayout.


Solution

  • Just as Jason said, your buttons are children of the Grid, and the Grid is a child of the StackLayout.

    You can get the Button with following code:

            var children = this.xGrpTabBtns.Children.Where(x => x is Grid); 
    
            foreach (var child in children) {
                if (child is Grid) {
                    var grid = (Grid)child;
    
                    var inerChildren = grid.Children.Where(x => x is Button);
                    foreach (var innerchild in inerChildren) {
    
                        if (innerchild is Button) { 
                            
                           System.Diagnostics.Debug.WriteLine("find one button here " + ((Button)innerchild).Text);
                        
                        }
                    }
                }
            }