[英]Xamarin Forms - DataTemplate from Nested Property within DataTemplate from Parent Object
[英]Xamarin Forms bind to parent property in DataTemplate
我无法绑定到 Xamarin 中的属性,并且由于某种原因无法使用 Microsoft 的文档来解决。
假设我有这个视图模型:
public class FooViewModel
{
public IEnumerable<string> Foos { get; set; }
public string SpecialFoo { get; set; }
}
在我看来,这是:
<StackLayout BindableLayout.ItemsSource="{Binding Foos}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding SpecialFoo}"/>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
我遇到了在 DataTemplate 中无法绑定到 FooViewModel 中的属性的问题。 如何将此绑定到 SpecialFoo?
这很简单,您只需要引用您的页面并告诉它在 VM 中而不是在 model 中查找此内容:
为您当前的 ContentPage 命名:
<ContentPage
...
x:Name="currentPage"/>
然后你的 label 看起来像:
<Label Text="{Binding BindingContext.SpecialFoo, Source={x:Reference currentPage}}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.