Xamarin ListView Dynamic ItemTemplate

本文介绍如何使用DataTemplateSelector在ListView中实现动态加载不同数据模板的方法。通过创建DynamicTemplateSelector类,根据不同数据类型选择对应的DataTemplate,实现了文本输入框与下拉选择框的动态切换。代码示例详细展示了Xamarin应用中UI数据绑定的具体实现。
摘要由CSDN通过智能技术生成

实现ListView动态加载不同的数据模板需要用到DataTemplateSelector

public class DynamicTemplateSelector: DataTemplateSelector
    {
        public DataTemplate TextTemplate { set; get; }
        public DataTemplate PickerTemplate { set; get; }

        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
        {
            var model = item as ItemModel;
            if (model.DataType == 1)
                return TextTemplate;
            else
                return PickerTemplate;

        }
    }
 public class ItemModel
    {
        public string Label { set; get; }
        public string Value { set; get; }
        public int DataType { set; get; }
    }

UI数据绑定

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:XamarinApp"
             xmlns:c="clr-namespace:XamarinApp.Controls"
             x:Class="XamarinApp.MainPage">

    <ContentPage.Resources>
        <DataTemplate x:Key="textTemplate">
            <ViewCell>
                <StackLayout Orientation="Horizontal" VerticalOptions="Center"  HorizontalOptions="FillAndExpand">
                    <Label Text="{Binding Label}" VerticalOptions="Center"/>
                    <Entry Text="{Binding Value}" Margin="10,0" HorizontalOptions="FillAndExpand"/>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
        <DataTemplate x:Key="pickertTemplate">
            <ViewCell>
                <StackLayout Orientation="Horizontal" VerticalOptions="Center" HorizontalOptions="FillAndExpand">
                    <Label Text="{Binding Label}" VerticalOptions="Center"/>
                    <Picker SelectedItem="{Binding Value,Mode=TwoWay}" Margin="10,0" HorizontalOptions="FillAndExpand">
                        <Picker.Items>
                            <x:String>男</x:String>
                            <x:String>女</x:String>
                        </Picker.Items>
                    </Picker>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
        <c:DynamicTemplateSelector x:Key="dynamicTemplate" TextTemplate="{StaticResource textTemplate}" PickerTemplate="{StaticResource pickertTemplate}">
            
        </c:DynamicTemplateSelector>
    </ContentPage.Resources>
    <StackLayout>
        <ListView ItemsSource="{Binding Items}" ItemTemplate="{StaticResource dynamicTemplate}" Margin="15,20">
            
        </ListView>
    </StackLayout>
</ContentPage>
public partial class MainPage : ContentPage
    {
        private List<ItemModel> items;
        public List<ItemModel> Items
        {
            get
            {
                return items;
            }

            set
            {
                items = value;
            }
        }
        public MainPage()
        {
            InitializeComponent();
            Items = new List<ItemModel>();
            Items.Add(new ItemModel() {DataType=1, Label="姓名",Value="" });
            Items.Add(new ItemModel() { DataType = 2, Label = "性别", Value = "男" });

            this.BindingContext = this;
        }

       
    }

用起来就是这么简单,噢耶!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值