Xamarin表单绑定-访问父属性 [英] Xamarin Forms Binding - access parent property
问题描述
我无法访问页面的ViewModel属性以将其绑定到IsVisible属性.如果不设置BindingContext,则只能绑定它.有没有办法在设置BindingContext的同时访问页面的viewmodel/root属性?
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?
页面XAML:
<eventViews:EventInfoWidget BindingContext="{Binding EventViewModel}" IsVisible="{Binding IsEventInfoWidgetEnabled}" />
<eventViews:AvailableShiftInfoWidget BindingContext="{Binding EventViewModel}" IsVisible="{Binding IsAvailableShiftInfoWidgetEnabled}"></eventViews:AvailableShiftInfoWidget>
ViewModel:
ViewModel:
public EventViewModel EventViewModel { get; }
public bool IsEventInfoWidgetEnabled => _IsEventInfoWidgetEnabled.Value;
public bool IsAvailableShiftInfoWidgetEnabled => _IsAvailableShiftInfoWidgetEnabled.Value;
IsVisibile属性只能绑定到EventViewModel对象属性,但是我想将其绑定到页面的viewmodel
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.
首先设置页面名称
<pages:AppContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
x:Name="ShiftPage">
之后,只需将IsVisible属性绑定到正确的源
After that just bind IsVisible Property to the right source
IsVisible="{Binding Source={x:Reference ShiftPage}, Path=BindingContext.IsEventInfoWidgetEnabled }"
这篇关于Xamarin表单绑定-访问父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!