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?
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.
Answers
The setter is never being hit, You need to set the public property not the private one.
Doing this, runs the set {} method, which will run the OnPropertyChanged event.
@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));
@seanyda I tried but no result,
my Page.Cs ;
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.
@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
'sInfoCustomer
'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
.thanks a lot ! i was making the same mistake ..this post solved my issue cheers !
choose binding mode one way
IsVisible="{Binding CorporateCustomer_IsVisible, Mode=OneWay}"