我一直在解决有关XAML中的数据绑定问题。绑定DataTemplate中的自定义控件
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Pedagouge.Views;assembly=Pedagouge"
x:Class="Pedagouge.Views.StudentGroupView">
<StackLayout>
<ListView x:Name="Students">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<local:PhotoView x:Name="Photo" Persona="{Binding}" />
<StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand" VerticalOptions="StartAndExpand">
<Label Text="{Binding FullName}" />
<Label Text="{Binding StudentIsAt.Group.Name}" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
我以为我有绑定的想法,但我有问题清楚地表明我不是。 不管怎么说,问题是,自定义控制PhotoView
似乎使用{Binding}
的财产Persona
结合上下文是PhotoView
的BindingContext
,而不是DataTemplate
的情况下,因为它是为Label
SA几排下来。
,如果获得WPF,我会只是改变了结合
Persona="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}"
而且很可能有固定的,但不会在这里工作(显然是没有定义的RelativeSource)。
怎么回事?我如何才能使Persona
与DataTemplate
的情况相关联,如Label
的情况那样?
如果你添加一个属性'Self {get {return this; }}'在你的viewmodel上,然后把PhotoView.Persona上的绑定改为'{Binding Self}'?另外..看看这个:http://stackoverflow.com/questions/22595388/binding-usercontrol-inside-a-listview-datatemplate-wpf – mrtig 2014-10-04 15:45:57
@mrtig听起来这样仍然会给我一个参考视图模型,而不是当前绑定到DataTemplate的项目 – fredrik 2014-10-04 15:49:04
是的,我想你是对的。您可以尝试绑定集合的每个成员上的“self”属性。 ..或者你可以尝试'ElementName'建议(在上面的链接中)。 – mrtig 2014-10-04 15:54:49