Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Hide Grid by Toggling a Switch - Data Binding

$
0
0

Hi All,

Building a project which extracts questions and question categories from a database and then builds out a series of Editors, each within a Grid. At the top I have a label and a switch with the intent that the grid can be collapsed using the switch. An example picture below.

Essentially, I'm having a heck of a time working out how to link the correct button with the correct Grid so that it hides. I built it out in Xaml OK using static values, but as I'm using questions from a database, I have to do this purely in C#. I was trying to use bindingcontext and setbinding - all to no luck.

The switch is categoryTitleSwitch. The element I want to toggle is categoryGrid.

Any tips would be great

                 // loop through categories
                foreach (var category in categories)
                {

                    // Category Label and visibility switch
                    var categoryTitleGrid = new Grid { };

                    // category title column definitions
                    categoryTitleGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                    categoryTitleGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

                    // category title row defitions
                    categoryTitleGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });

                    // category title label
                    var categoryTitleLabel = new Label { Text = category.name, Style=Device.Styles.SubtitleStyle };
                    var categoryTitleLabelLayout = new StackLayout { Orientation = StackOrientation.Vertical };
                    categoryTitleLabelLayout.Children.Add(categoryTitleLabel);

                    // category title switch
                    var categoryTitleSwitchLayout = new StackLayout { Orientation = StackOrientation.Vertical };
                    var categoryTitleSwitch = new Switch {  IsToggled = true };

                    categoryTitleSwitchLayout.Children.Add(categoryTitleSwitch);

                    // add layouts to grid
                    categoryTitleGrid.Children.Add(categoryTitleLabelLayout, 0, 0);
                    categoryTitleGrid.Children.Add(categoryTitleSwitchLayout, 1, 0);

                    // add now grid to the rest of the layout to get in right spot
                    parentLayout.Children.Add(categoryTitleGrid);

                    // wrapping grid - set early for binding context
                    var categoryGrid = new Grid { Padding = new Thickness(20, 20, 20, 40) };

                    categoryGrid.BindingContext = categoryTitleSwitch.IsToggled;
                    //categoryGrid.BindingContext = new { IsVisible = categoryTitleSwitch.IsToggled };
            //categoryGrid.SetBinding(Grid.IsVisibleProperty, new Binding());

                    // single columns
                    categoryGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

                    // process each question
                    foreach (var question in questions)
                    {

            // processes each question
                    }

                    parentLayout.Children.Add(categoryGrid);
                }

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>