Fra*_*lue 5 datatemplate xamarin.forms
我想知道是否有办法获取对 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 中存在与此类等效的类。
小智 5
是的,可以访问DataTemplate在运行时使用创建的视图。来自 XAMLBindingContextChanged的视图的钩子事件DataTemplate。在事件回调中,DataTemplate可以使用 sender 参数访问从中创建的视图。您需要对发件人进行类型转换才能访问视图,因为发件人被装箱为对象类型。
或者,您可以使用 DataTemplate 选择器根据您的对象创建视图。
| 归档时间: |
|
| 查看次数: |
2654 次 |
| 最近记录: |