Xamarin.Forms在列表视图中显示特定项目的不同数据模板 [英] Xamarin.Forms show a different datatemplate for a specific item in listview

查看:84
本文介绍了Xamarin.Forms在列表视图中显示特定项目的不同数据模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Xamarin.Forms和.Net Standard开发动态应用程序。
我正在使用MVVM作为代码模式。视图后面没有代码。

I'm currently developing a dynamic app using Xamarin.Forms with .Net Standard. I am using MVVM as code pattern. No code behind the view.

视图/页面的内容是绑定到 TemplateItem 列表的列表视图对象。每个列表视图项 TemplateItem 都应该看起来一样(作为文章)。但是,当 TemplateItem 的属性 BlockType slideshow 时,

The content of the view/page is a listview bound to a list of TemplateItem objects. Every listview item, TemplateItem, should look the same (as an article). But when the property BlockType of the TemplateItem is slideshow, the listview must look different by using another data template.

当对象的属性不同时,如何为列表视图项使用另一个数据模板?

How can I use another data template for a listview item when a property of the object is different?

这是我的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>

这里是模型类:

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:

我要完成的任务的WireFrame

推荐答案

而不是使用DataTemplate,请尝试使用DataTemplateSelector。这样就可以为不同的对象设置不同的模板。
参考链接: https ://docs.microsoft.com/zh-CN/xamarin/xamarin-forms/app-fundamentals/templates/data-templates/selector

Instead of using DataTemplate, try to use 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

这篇关于Xamarin.Forms在列表视图中显示特定项目的不同数据模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆