IsVisible Binding Not Woking

RamazanRamazan Member ✭✭
edited July 2018 in Xamarin.Forms

I have an app that customer type to searchbar and data(just 3 of them) is written(Listview) from webAPI to screen according to search parameter. When customer select the coming data from that screen i direct the customer to detail page. Customer can see all the information that comes from webAPI. The data that comes from webAPI has only one model. I filter that model according to my needs at my backend.

That detail page can have different views according to my filter. So i try to write all data to XAML file and use isvisible binding to different view.

My Code: I have 3 different stack layout inside a stacklayout.

   <StackLayout VerticalOptions="Center" HorizontalOptions="CenterAndExpand" Padding="20,30,20,30"  BindingContext="{Binding InfoCustomer}">

            <StackLayout  x:Name="Layout_PersonelCustomer" IsVisible="{Binding PersonelCustomer_IsVisible}">
        <grid>
            .
            .
                  </grid>
            </stacklayout>
            <StackLayout  x:Name="Layout_CorporateCustomer" IsVisible="{Binding CorporateCustomer_IsVisible}">
        <grid>
            .
            .
                 </grid>
    </stacklayout>
      <StackLayout IsVisible="{Binding ForeignCustomer_IsVisible}">
        <grid>
            .
            .
                 </grid>
    </stacklayout>
</stacklayout>

My ViewModel;

  public class SearchModelView : INotifyPropertyChanged
{
    bool _personelCustomer_IsVisible;
    bool _corporateCustomer_IsVisible;
    bool _foreignCustomer_IsVisible;

   public bool PersonelCustomer_IsVisible
    {
        get { return _personelCustomer_IsVisible; }
        set
        {
            CheckCustomerType();
            _personelCustomer_IsVisible = value;
            OnPropertyChanged(nameof(_personelCustomer_IsVisible));
        }
    }
    public bool CorporateCustomer_IsVisible
    {
        get { return _corporateCustomer_IsVisible; }
        set
        {
            CheckCustomerType();
            _corporateCustomer_IsVisible = value;
            OnPropertyChanged(nameof(_corporateCustomer_IsVisible));
        }
    }
    public bool ForeignCustomer_IsVisible
    {
        get { return _foreignCustomer_IsVisible; }
        set
        {
            CheckCustomerType();
            _foreignCustomer_IsVisible = value;
            OnPropertyChanged(nameof(_foreignCustomer_IsVisible));
        }
    }
 private void PersonelCustomerEnabled()
    {
        _personelCustomer_IsVisible = true;
        _corporateCustomer_IsVisible = false;
        _foreignCustomer_IsVisible = false;
    }

    private void CorporateCustomerEnabled()
    {
        _personelCustomer_IsVisible = false;
        _corporateCustomer_IsVisible = true;
        _foreignCustomer_IsVisible = false;
    }

    private void ForeignCustomerEnabled()
    {
        _personelCustomer_IsVisible = false;
        _corporateCustomer_IsVisible = false;
        _foreignCustomer_IsVisible = true;
    }

    private void CheckCustomerType()
    {
        var _customerType = _customerModel.CustomerType;

        var _customerTypeInt = (int)Convert.ToInt16(_customerType);

        if (_customerTypeInt == 1)
        {
            PersonelCustomerEnabled();
        }

        else if (_customerTypeInt == 2)
        {
            CorporateCustomerEnabled();
        }

        else if (_customerTypeInt == 3)
        {
            ForeignCustomerEnabled();
        }
    }


    public void OnPropertyChanged(string propertyName)
    {
        this.PropertyChanged?.Invoke(this, new
            PropertyChangedEventArgs(propertyName));
    }
}

My XAML.cs;

     public UpdatePage (CustomerModel _customerModel)
      {

        this.customerModel = _customerModel;
        BindingContext = new SearchModelView(customerModel);
      //  BindingContext = new DetailViewModel(customerModel);
           Title = "UpdatePage";
        InitializeComponent ();

    }

I try debugging but break point don't hit modelview Plesase help why my binding to isvisible not working?

Best Answer

Answers

  • RamazanRamazan Member ✭✭

    @seanyda Thanks for suggestion now breakpoint started to work but still their is no binding with frontent. OnPropertyChange event works internally. I mean with your suggestion setter is hitted by breakpoint cause of Capsulation-Incapsulation but no effect on frontend. Actullay i want to connected with frontend.

  • seanydaseanyda GBMember ✭✭✭✭✭

    @Ramazan said:
    @seanyda Thanks for suggestion now breakpoint started to work but still their is no binding with frontent. OnPropertyChange event works internally. I mean with your suggestion setter is hitted by breakpoint cause of Capsulation-Incapsulation but no effect on frontend. Actullay i want to connected with frontend.

    You need to replace all the private fields with public ones including these:

    OnPropertyChanged(nameof(_personelCustomer_IsVisible));

    You don't want to be using any private fields, this is an reference but you should always be calling the propertys with the public ones like this:

    OnPropertyChanged(nameof(PersonelCustomer_IsVisible));

  • RamazanRamazan Member ✭✭

    @seanyda I tried but no result,

     public bool PersonelCustomerIsVisible
        {
            get { return _personelCustomerIsVisible; }
            set
            {
    
                _personelCustomerIsVisible = value;
                OnPropertyChanged(nameof(PersonelCustomerIsVisible));
            }
        }
        public bool CorporateCustomerIsVisible
        {
            get { return _corporateCustomerIsVisible; }
            set
            {
    
                _corporateCustomerIsVisible = value;
                OnPropertyChanged(nameof(CorporateCustomerIsVisible));
            }
        }
        public bool ForeignCustomerIsVisible
        {
            get { return _foreignCustomerIsVisible; }
            set
            {
    
                _foreignCustomerIsVisible = value;
                OnPropertyChanged(nameof(ForeignCustomerIsVisible));
            }
        }
    
             public void PersonelCustomerEnabled()
        {
            PersonelCustomerIsVisible = true;
            CorporateCustomerIsVisible = false;
            ForeignCustomerIsVisible = false;
    
    }
    
        public void CorporateCustomerEnabled()
        {
            PersonelCustomerIsVisible = false;
            CorporateCustomerIsVisible = true;
            ForeignCustomerIsVisible = false;
    
        }
    
        public void ForeignCustomerEnabled()
        {
            PersonelCustomerIsVisible = false;
            CorporateCustomerIsVisible = false;
            ForeignCustomerIsVisible = true;
        }
    
    
     public void CheckCustomerType()
        {
            var _customerType = _customerModel.CustomerType;
    
            var _customerTypeInt = (int)Convert.ToInt16(_customerType);
    
            if (_customerTypeInt == 1)
            {
                PersonelCustomerEnabled();
            }
    
            else if (_customerTypeInt == 2)
            {
                CorporateCustomerEnabled();
            }
    
            else if (_customerTypeInt == 3)
            {
                ForeignCustomerEnabled();
            }
        }
    

    my Page.Cs ;

    public UpdatePage (CustomerModel _customerModel)
            {
    
            this.customerModel = _customerModel;
            BindingContext = new SearchModelView(customerModel);
          //  BindingContext = new DetailViewModel(customerModel);
            Title = "CS.CRM Müşteri Bilgi Güncelleme";
           // CheckCustomerType(_customerModel);
    
    
            InitializeComponent ();
    
        }
    

    As my experience(i am newbee) when bind was successful, Breakpoint hit directly to the name . I mean if i put a breakpoint at "PersonelCustomerIsVisible" bool value, it should directly hit it.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @Ramazan Each Bindable Object has a BindingContext. System will search it from lower to upper. In your case, you set your page's BindingContext in your code behind, but you also set your three stacks's super stack layout's binding context to a InfoCustomer: <StackLayout VerticalOptions="Center" HorizontalOptions="CenterAndExpand" Padding="20,30,20,30" BindingContext="{Binding InfoCustomer}">.
    Then each of your single stack layout which you want to modify its visibility has been bound to your SearchModelView's InfoCustomer's property.
    From your SearchModelView class, system can't find this property. Also can't get this property's children called PersonelCustomerIsVisible...
    Remove this Binding context in your XAML. And correct your binding path to your view model's property name. Since I notice you have changed them to PersonelCustomerIsVisible, CorporateCustomerIsVisible, ForeignCustomerIsVisible.

  • @seanyda said:
    The setter is never being hit, You need to set the public property not the private one.

     private void PersonelCustomerEnabled()
        {
            PersonelCustomer_IsVisible = true;
            CorporateCustomer_IsVisible = false;
            ForeignCustomer_IsVisible = false;
        }
    

    Doing this, runs the set {} method, which will run the OnPropertyChanged event.

    thanks a lot ! i was making the same mistake ..this post solved my issue cheers !

Sign In or Register to comment.