[英]Xamarin.Forms show a different datatemplate for a specific item in listview
I'm currently developing a dynamic app using Xamarin.Forms with .Net Standard. 我目前正在使用带有.Net Standard的Xamarin.Forms开发动态应用程序。 I am using MVVM as code pattern. 我使用MVVM作为代码模式。 No code behind the view. 视图后面没有代码。
The content of the view/page is a listview bound to a list of TemplateItem
objects. 视图/页面的内容是绑定到TemplateItem
对象列表的列表视图。 Every listview item, TemplateItem
, should look the same (as an article). 每个列表视图项TemplateItem
应该看起来都一样(作为文章)。 But when the property BlockType
of the TemplateItem
is slideshow
, the listview must look different by using another data template. 但是当TemplateItem
的属性BlockType
是slideshow
,列表视图必须使用另一个数据模板看起来不同。
How can I use another data template for a listview item when a property of the object is different? 当对象的属性不同时,如何为列表视图项使用其他数据模板?
Here is my xaml: 这是我的xaml:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:App1"
x:Class="App1.MainPage"
NavigationPage.HasBackButton="True"
NavigationPage.HasNavigationBar="True"
Title="Overview">
<StackLayout >
<ActivityIndicator IsRunning="{Binding IsBusy}"
HorizontalOptions="CenterAndExpand" IsVisible="{Binding IsBusy}"/>
<ScrollView>
<StackLayout>
<ListView ItemsSource="{Binding LstTemplateList}" SeparatorVisibility="Default" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate x:Name="DTArticle">
<ViewCell>
<StackLayout>
<Label Text="{Binding Title}" FontSize="Large" />
<TextCell Text="ArticleDescription"/>
</StackLayout>
</ViewCell>
</DataTemplate>
<DataTemplate x:Name="DTSlideShow">
<ViewCell>
<!-- another DT for a slideshow -->
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>
</StackLayout>
</ContentPage>
Here is the model class: 这是模型类:
public class TemplateItem
{
public int Id { get; set; }
public String BlockType { get; set; }
public String Title { get; set; }
public String ArticleDescription { get; set; }
public List<String> LstImagePathsForSlideshow { get; set; }
}
Here is a wireframe to show you what I am trying to accomplish: 这是一个线框,向您展示我想要实现的目标:
Instead of using DataTemplate, try to use DataTemplateSelector. 而不是使用DataTemplate,尝试使用DataTemplateSelector。 So that you can set different template for different objects. 这样您就可以为不同的对象设置不同的模板。 Reference Link : https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector 参考链接: https : //docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.