Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing isVisible property of Xamarin Forms XAML buttons

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?

like image 352
dease Avatar asked Apr 23 '15 12:04

dease


2 Answers

Change your code in xmal file and write properties for start and stop button

<Button x:Name="start_btn" Clicked="startPanic" IsVisible="{Binding IsStartVisible}">
    <Button.Text>START</Button.Text>
</Button>

<Button x:Name="stop_btn" IsVisible="{Binding IsStopVisible}">
    <Button.Text>STOP</Button.Text>
</Button>

In ViewModel write following property and similar for start button and set IsStopVisible =true/false based on your logic

   private bool _isStopVisible;

    public bool IsStopVisible{
        get {
            return _isStopVisible;
        }
        set {
            _isStopVisible= value;
            RaisePropertyChanged ("IsStopVisible");
        }
    }
like image 109
Drw Avatar answered Oct 22 '22 14:10

Drw


Maybe I'm late but I was searching this too without success. This may be useful for someone.

objectView.SetValue(IsVisibleProperty, false); // the view is GONE, not invisible
objectView.SetValue(IsVisibleProperty, true);
like image 28
atapi19 Avatar answered Oct 22 '22 14:10

atapi19