Home > Back-end >  How to make a button invisible even when I refresh a page depending on a label content in xamarin fo
How to make a button invisible even when I refresh a page depending on a label content in xamarin fo

Time:05-20

When I click on the button it becomes hidden, however when I refresh the page it is still there. How to make it stay hidden depending on a content in a label?

 private void Button3_Clicked(object sender, EventArgs e)
        {
            App.products[Index].Quantity  ;
            Button btn = (Button)sender;
            if (btn.IsVisible)
            { btn.IsVisible = false; }
            else
            {
                btn.IsVisible = false;

            }
        }

I want this Button to stay hidden when the page is refreshed depending on this value App.products[Index].Quantity. When I click on the Button it becomes from 0 to 1 and I want if it is not 0 the Button to be hidden.

CodePudding user response:

In YourPage.xaml:

    <Button IsVisible={Binding ButtonIsVisible} ... />

In YourPage.xaml.cs:

public YourPage()
{
    InitializeComponent();
    ...
    BindingContext = this;
}

// NOTE: `=>` not `=`. To be a property; expression evaluated every time it is needed.
public bool ButtonIsVisible => App.products[Index].Quantity == 0;

private void Button3_Clicked(object sender, EventArgs e)
{
    App.products[Index].Quantity  ;
    // So xaml sees the change.
    OnPropertyChanged(nameof(ButtonIsVisible));
}

For more information, google xamarin binding.

  • Related