I am trying to dynamically show/hide button inside Xamarin Forms ContentPage. I have two buttons in my XAML code:
<StackLayout Orientation="Vertical">
<Button x:Name="start_btn" Clicked="startPanic">
<Button.Text>START</Button.Text>
</Button>
<Button x:Name="stop_btn" IsVisible="false">
<Button.Text>STOP</Button.Text>
</Button>
</StackLayout>
Corresponding C# code:
public partial class PanicPage : ContentPage
{
private Button startBtn;
private Button stopBtn;
public PanicPage ()
{
InitializeComponent ();
startBtn = this.FindByName<Button> ("start_btn");
stopBtn = this.FindByName<Button> ("stop_btn");
}
private void startPanic(object sender, EventArgs args){
Device.BeginInvokeOnMainThread (() => {
startBtn.IsVisible = false;
stopBtn.IsVisible = true; // DOESN'T WORK, button still will be hidden
});
}
}
When I set isVisible property in XAML, it doesn't react for any property change in event method (startPanic). How can I fix it?
Use the Visibility property of view.
for example if u want to make your button invisible you can do
Change your code in xmal file and write properties for start and stop button
In ViewModel write following property and similar for start button and set IsStopVisible =true/false based on your logic
private bool _isStopVisible;
It should work just fine. I copied your code and cleaned it up a bit, it shows the STOP button, then I
A few remarks:
<Button Text="X"/>
, it's easier to readHere's the XAML, same as yours just tighter and added Margin so the button is visible
And the code behind:
Maybe I'm late but I was searching this too without success. This may be useful for someone.