简体   繁体   中英

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

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 }"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM