好吧,这比问题更令人烦恼。没有错误
页
<ContentPage
...
x:Name="This"
//hack to have typed xaml at design-time
BindingContext="{Binding Source={x:Static viewModels:ViewModelLocator.ChooseTargetLocationVm}}"
子视图
<views:ProductStandardView
...
BindingContext="{Binding Product}">
<Grid.Triggers>
<DataTrigger
Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"
TargetType="Grid"
Value="true">
<Setter Property="BackgroundColor" Value="{StaticResource WarningColor}" />
</DataTrigger>
</Grid.Triggers>
从的源引用绑定到时,我收到 XAML“警告”BindingContext
This
无法解析“对象”类型的数据上下文中的属性“IsVacate”
Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"
显然BindingContext是一个对象并且没有类型。然而,上面的代码编译并工作
我想要做的是投射它,首先因为我有强迫症,但主要是因为它很容易在 IDE 页面通道栏上发现真正的问题
以下似乎合乎逻辑但不起作用
Binding="{Binding Path=BindingContext.(viewModels:ChooseTargetLocationVm.IsVacate),
Source={x:Reference This}}"
在输出中我得到
[0:] 绑定:'(
viewModels:ChooseTargetLocationVm
'在''上找不到Inhouse.Mobile.Standard.ViewModels.ChooseTargetLocationVm
属性,目标属性:'Inhouse.Mobile.Standard.Views.ProductStandardView.Bound
'
我理解错误,但我还能如何投射?
只是为了愚蠢,显然以下内容不会编译
Binding="{Binding Path=((viewModels:ChooseTargetLocationVm)BindingContext).IsVacate, Source={x:Reference This}}"
那么有没有办法将BindingContext转换为ViewModel ,以便在设计时键入任何SubProperty引用?
更新
这与内部相关,DataTemplate
或者在这种情况下,当控件有自己的控件时,BindingContext
这就是为什么我需要使用Source={x:Reference This}
来定位页面。
注意:<ContentPage.BindingContext>
对我不起作用,因为我使用的是 prism 和 unity 并且在初始测试中它似乎不能很好地使用默认构造函数,尽管我可能会再玩一些