简体   繁体   English

Xamarin Forms Binding - 访问父属性

[英]Xamarin Forms Binding - access parent property

I cannot access the Page's ViewModel property in order to bind it to IsVisible Property. 我无法访问Page的ViewModel属性以将其绑定到IsVisible属性。 I can only bind it if I don't set the BindingContext. 如果我没有设置BindingContext,我只能绑定它。 Is there a way I can access the page's viewmodel/root property while also setting the BindingContext? 有没有办法在设置BindingContext的同时访问页面的viewmodel / root属性?

Page XAML: 页面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 IsVisibile属性只能绑定到EventViewModel对象属性,但我想将它绑定到页面的viewmodel

  • IsEventInfoWidgetEnabled IsEventInfoWidgetEnabled
  • IsAvailableShiftInfoWidgetEnabled 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属性绑定到正确的源

IsVisible="{Binding Source={x:Reference ShiftPage}, Path=BindingContext.IsEventInfoWidgetEnabled }"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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