Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Binding - access parent property

I cannot access the Page's ViewModel property in order to bind it to IsVisible Property. I can only bind it if I don't set the BindingContext. Is there a way I can access the page's viewmodel/root property while also setting the BindingContext?

Page XAML:

<eventViews:EventInfoWidget BindingContext="{Binding EventViewModel}" IsVisible="{Binding IsEventInfoWidgetEnabled}" />
<eventViews:AvailableShiftInfoWidget BindingContext="{Binding EventViewModel}" IsVisible="{Binding IsAvailableShiftInfoWidgetEnabled}"></eventViews:AvailableShiftInfoWidget>

ViewModel:

public EventViewModel EventViewModel { get; }
public bool IsEventInfoWidgetEnabled => _IsEventInfoWidgetEnabled.Value;
public bool IsAvailableShiftInfoWidgetEnabled => _IsAvailableShiftInfoWidgetEnabled.Value;

The IsVisibile Property can only be bind to the EventViewModel Object Properties, but I would like to bind it to the page's viewmodel

  • IsEventInfoWidgetEnabled
  • IsAvailableShiftInfoWidgetEnabled
like image 689
Iliut Andrei Avatar asked Jun 11 '18 13:06

Iliut Andrei


1 Answers

Found the solution, you have to specify the source and then set the path to the property.

First set the name of the page

<pages:AppContentPage 
xmlns="http://xamarin.com/schemas/2014/forms" 
x:Name="ShiftPage">

After that just bind IsVisible Property to the right source

IsVisible="{Binding Source={x:Reference ShiftPage}, Path=BindingContext.IsEventInfoWidgetEnabled }"
like image 100
Iliut Andrei Avatar answered Oct 07 '22 11:10

Iliut Andrei