我想知道是否有办法获取对 Xamarin.Forms 中 ListView 中 DataTemplate 内视图的引用。假设我有这个 xaml:
<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>
Run Code Online (Sandbox Code Playgroud)
我希望能够在 ListView 的每一行中获取对名为“ProductStackLayout”的 StackLayout 的引用。我需要在页面出现时执行此操作,以动态操作其内容(对于数据绑定无法实现的某些内容),因此我无法利用在源自 DataTemplate 中的元素的事件处理程序中传递的视图引用本身就像 ItemTapped 或类似的。
据我所知,在 WPF 或 UWP 中,可以在 VisualTreeHelper 类的帮助下实现类似的功能,但我不相信 Xamarin.Forms 中存在与此类等效的类。