Problems with IsVisible Binding on Android

Hi!

I'm having problems to make some buttons pop up after a button click. I'm trying to bind the IsVisible of these popping up buttons to a property on my View Model. While others properties update properly, the IsVisible won't.

My ViewModel:

`
public class GeoValorVM : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;

    public Single _Comprimento = 5;
    public Single geo_Comprimento
    {
        get
        {
            return _Comprimento;
        }
        set
        {
            _Comprimento = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("geo_Comprimento"));
            }
        }
    }

    public Single _CargaADM;
    public Single geo_CargaADM
    {
        get
        {
            return _CargaADM;
        }
        set
        {
            _CargaADM = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("geo_CargaADM"));
            }
        }
    }

    public bool _Foco;
    public bool Foco
    {
        get
        {
            return _Foco;
        }
        set
        {
            _Foco = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Foco"));

            }

        }
    }

    public ICommand Seleção { get; set; }

    public GeoValorVM ()
    {

        Seleção = new Command<bool>(GeoValorSeleciona);

    }

    void GeoValorSeleciona(bool value)
    {
        Foco = value;
    }

 }`

My Command:

<Button x:Name="gvl_Comprimento" BackgroundColor="White" Text="{Binding geo_Comprimento, StringFormat='{0:0.00}'}" FontSize="Medium" HorizontalOptions="Start" WidthRequest="140" Command="{Binding Seleção}"> <Button.CommandParameter> <x:Boolean>True</x:Boolean> </Button.CommandParameter> </Button>

The pop up button:

<Button x:Name="btn_InteiroAumentar2" HorizontalOptions="FillAndExpand" VerticalOptions="End" BackgroundColor="ForestGreen" IsVisible="{Binding Foco}"> <Button.Text> /\ /\ </Button.Text> </Button>

The Binding Context of the buttons are defined on different stacklayouts, both like this:

<StackLayout.BindingContext> <local:GeoValorVM/> </StackLayout.BindingContext>

The other properties update correctly, but I can't figure how to update the IsVisible property of the buttons according to the ViewModel's Foco property. Any guidance is appreciated!

Posts

  • RaelsonCraftzRaelsonCraftz BRMember ✭✭

    I quit trying to make the button visible through the 'Foco' property. I Implemented this feature through Clicked events and it's working properly. However, now I noticed that when I try to change the 'geo_Comprimento' property by clicking on buttons other than the one on which I have Text="{Binding geo_Comprimento}", the Text won't update, even though the property itself is being changed. So it appears that the issue I was facing is not related to the 'Foco' property itself, but the fact that it was being changed by a command in another control. Could this be a bug? Or I'm simply doing something wrong?

  • RaelsonCraftzRaelsonCraftz BRMember ✭✭
    edited August 2017

    I've found what was the problem. The buttons are in different StackLayouts, and I was setting the BindingContext of both StackLayouts like this:

    ``

    <StackLayout1.BindingContext>

    <StackLayout1.BindingContext>


    <StackLayout2.BindingContext>

    <StackLayout1.BindingContext>

    ``

    That's it, that was the problem. When I set the BindingContext on the ContentPage that has both StackLayouts as children, the Commands from controls that were on different StackLayouts started to update properly. Before, only the Commands from controls in the same StackLayout were updating accordingly. Even so, it eludes why the PropertyChangedEventHandler wasn't working when the BindingContext is set on each StackLayout for the same ViewModel...maybe it's a bug?

  • RaelsonCraftzRaelsonCraftz BRMember ✭✭

    I've actually found the problem. It happens that I was setting the BindingContext of both StackLayouts like this:

    `

    <ContentPage>
    
        <StackLayout1>
            <StackLayout1.BindingContext>
                <local:ViewModel/>
            <StackLayout1.BindingContext>
        <StackLayout1>
    
        <StackLayout2>
            <StackLayout2.BindingContext>
                <local:ViewModel/>
            <StackLayout2.BindingContext>
        <StackLayout2>
    <ContentPage>
    

    `

    That's it, that was the problem. I solved the issue by setting the BindingContext on the ContentPage, instead of each StackLayout. However, it eludes me how this is actually a problem. Maybe it's a bug?

  • RaelsonCraftzRaelsonCraftz BRMember ✭✭
    edited August 2017

    [duplicate]

Sign In or Register to comment.