我希望知道在Xamarin.Forms的ListView中,如何获取DataTemplate中的视图的引用。
假设我有以下XAML代码:
我希望能够在ListView的每一行中获取名为“ProductStackLayout”的StackLayout的引用。当页面出现时,我需要这样做来动态操作其内容(对于无法通过数据绑定实现的某些内容),因此我无法利用从DataTemplate本身的元素发起的事件处理程序传递的视图引用,例如ItemTapped或类似事件。
据我所知,在WPF或UWP中,可以借助VisualTreeHelper类实现这样的功能,但我不认为Xamarin.Forms中有这个类的等效物。
<ListView x:Name="ProductList" ItemsSource="{Binding Products}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout BackgroundColor="#eee" x:Name="ProductStackLayout"
Orientation="Vertical" Padding="5" Tapped="ListItemTapped">
<Label Text="{Binding Name}"
Style="{DynamicResource ProductPropertiesStyle}"/>
<Label Text="{Binding Notes}" IsVisible="{Binding HasNotes}"
Style="{DynamicResource NotesStyle}"
/>
<Label Text="{Binding Date}"
Style="{DynamicResource DateStyle}"
/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我希望能够在ListView的每一行中获取名为“ProductStackLayout”的StackLayout的引用。当页面出现时,我需要这样做来动态操作其内容(对于无法通过数据绑定实现的某些内容),因此我无法利用从DataTemplate本身的元素发起的事件处理程序传递的视图引用,例如ItemTapped或类似事件。
据我所知,在WPF或UWP中,可以借助VisualTreeHelper类实现这样的功能,但我不认为Xamarin.Forms中有这个类的等效物。