xamarin control changing button invisible true false in code

tim 120 Reputation points
2024-04-26T00:40:13.3866667+00:00

How can I control if a button is visible in code.

<buttons:BindableButton BindingContext="{Binding  Close_Page_Clicked}"


                                    HorizontalOptions="EndAndExpand"  IsVisible="{Binding IsReturnVisible}"

                                    BackgroundColor="White" TextColor="Black" BorderWidth="1"  BorderColor="Black" />


   

    private bool _isReturnVisible = false;   // button is visible when page loads

    public bool IsReturnVisible

    {

        get => _isReturnVisible;

        set

        {

            SetProperty(ref _isReturnVisible, value);

        }

    }
 _isReturnVisible = false; // also does not work

TIA

Tim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,627 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,656 Reputation points Microsoft Vendor
    2024-04-26T05:39:29.7133333+00:00

    Hello,

    Because you have set the BindingContext="{Binding Close_Page_Clicked}" for your button and

    you have set the   IsVisible="{Binding IsVisible}" in your BindableButton.xaml.

    If you need to make the BindableButton to disappear, you need to change it in your ViewModel.

    For example, You can set the false in the CollectionPageModel.cs 's constructor.

    ButtonModel buttonModel=   new ButtonModel("Close App", CloseAppAction, false); ;
    Close_App_Clicked = buttonModel;
    

    If you change it at runtime, set the buttonModel.IsVisible = true; in your background code.

    Xamarin support will end on May 1, 2024, it's recommended that you migrate your Xamarin app to .NET MAUI. Please see Xamarin official support policy .

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful