I have a dynamic control where being created when the page is loaded, however, I wanted to include a button also wherein it has a capability to show and hide the stack layout whenever I click it, but I'm not sure how to implement it correctly, please see my code.
StackLayout commentStack = new();
var button = new Button();
button.Text = " Add a Comment";
button.WidthRequest = 100;
button.Clicked = (sender, e) => OnButtonClicked(commentStack);
var stackGrid = new Grid()
{
RowDefinitions = new()
{
new RowDefinition() { Height = new GridLength(2, GridUnitType.Auto) },
new RowDefinition() { Height = new GridLength(2, GridUnitType.Auto) },
new RowDefinition() { Height = new GridLength(2, GridUnitType.Auto) },
},
ColumnDefinitions = new()
{
new ColumnDefinition(),
}
};
var commentlabel = new Label();
commentlabel.Text = Translator.Section_Comments;
commentlabel.FontSize = 17;
commentlabel.FontAttributes = FontAttributes.Bold;
var frame = new Frame();
var editor = new Editor();
frame.BorderColor = Color.LightGray;
frame.Padding = 1;
frame.HasShadow = false;
editor.IsEnabled = !this.IsReadOnly;
editor.FontSize = 17;
editor.HeightRequest = 150;
editor.TextChanged = (sender, e) => OnEditorCommentTextChanged(question, editor.Text);
frame.Content = editor;
stackGrid.Margin = new Thickness(0, 20, 0, 0);
stackGrid.Children.Add(button);
stackGrid.Children.Add(commentlabel, 0, 1);
stackGrid.Children.Add(frame, 0, 2);
commentStack.Children.Add(stackGrid);
stack.Children.Add(commentStack);
this.Children.Add(stack);
private void OnButtonClicked(StackLayout stack)
{
}
CodePudding user response:
Use the IsVisible
property
stackGrid.IsVisible = !stackGrid.IsVisible;