How I can switch the text of my button between "Show Comment" and "Hide Comment" whenever I click the button?
private void GenerateCommentContent()
{
var button = new Button();
button.Text = question.Comment == null ? $" {Translator.Buttons_AddComment}" : $"{Translator.Buttons_ShowComment}";
button.WidthRequest = 100;
button.Clicked = (sender, e) => OnButtonClicked(commentlabel, frame, button);
.......
}
private void OnButtonClicked(Label label, Frame frame, Button button)
{
label.IsVisible = !label.IsVisible;
frame.IsVisible = !frame.IsVisible;
button.Text = $"{Translator.Buttons_HideComment}";
}
So basically the first GenerateCommentContent will render a dynamic button, label and editor (comment textbox). If the question.Comment is null, the first text of the button is "Add a Comment", but if there is a data, the button should switch to "Hide Comment" and "Show Comment" only every time the button is click.
CodePudding user response:
Create a simple bool value to store the current state:
private bool commentShown = true;
private void OnButtonClicked(Label label, Frame frame, Button button)
{
label.IsVisible = !label.IsVisible;
frame.IsVisible = !frame.IsVisible;
commentShown = !commentShown;
button.Text = commentShown ? $"{Translator.Buttons_HideComment}" : $"{Translator.Buttons_ShowComment}";
}