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

查看:25
本文介绍了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 都应该看起来一样(作为一篇文章).但是当TemplateItemBlockType属性为slideshow时,listview必须使用另一个数据模板看起来不同.

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:

我想要完成的线框

推荐答案

不要使用 DataTemplate,尝试使用 DataTemplateSelector.以便您可以为不同的对象设置不同的模板.参考链接:https://docs.microsoft.com/en-us/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天全站免登陆