C# Xamarin表单绑定到DataTemplate中的父属性
我在绑定到Xamarin中的属性时遇到问题,并且由于某些原因,无法使用Microsoft的文档解决该问题 假设我有这个viewmodel:C# Xamarin表单绑定到DataTemplate中的父属性,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我在绑定到Xamarin中的属性时遇到问题,并且由于某些原因,无法使用Microsoft的文档解决该问题 假设我有这个viewmodel: public class FooViewModel { public IEnumerable<string> Foos { get; set; } public string SpecialFoo { get; set; } } 公共类FooViewModel { 公共IEnumerable Foos{get;set;} 公共
public class FooViewModel
{
public IEnumerable<string> Foos { get; set; }
public string SpecialFoo { get; set; }
}
公共类FooViewModel
{
公共IEnumerable Foos{get;set;}
公共字符串SpecialFoo{get;set;}
}
我认为:
<StackLayout BindableLayout.ItemsSource="{Binding Foos}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding SpecialFoo}"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
我遇到的问题是,在DataTemplate中,我无法绑定到FooViewModel内的属性。如何将此绑定到SpecialFoo?很简单,您只需要一个对页面的引用,并告诉它在VM中而不是在模型中查找: 为当前ContentPage指定一个名称:
<ContentPage
...
x:Name="currentPage"/>
然后,您的标签将类似于:
<Label Text="{Binding BindingContext.SpecialFoo, Source={x:Reference currentPage}}"/>
我的英雄!整个上午我都快疯了,谢谢。很乐意帮忙:)