Skip to content

SyncfusionExamples/itemtemplate-selector-listview-xamarin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

itemtemplate-selector-listview-xamarin

ItemTemplateSelector listview xamarin

Sample

<ContentPage.Resources>
    <ResourceDictionary>
        <local:MyDataTemplateSelector x:Key="MessageTemplateSelector"/>
    </ResourceDictionary>
</ContentPage.Resources>
<Grid>
    <sync:SfListView x:Name="ListView" 
                        ItemTemplate="{StaticResource MessageTemplateSelector}" 
                        ItemsSource="{Binding Messages}"
                        ItemSize="105"
                        SelectionMode="None"
                        BackgroundColor="#FFFAF0"/>
</Grid>

Incoming Cell:
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="DataTemplateSelector.IncomingViewCell">
        <code>
        . . .
        . . .
        <code>
</ViewCell>

Outgoing cell:
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="DataTemplateSelector.OutgoingViewCell">
        <code>
        . . .
        . . .
        <code>
</ViewCell>

DataTemplateSelector:

class MyDataTemplateSelector : Xamarin.Forms.DataTemplateSelector
{
    public DataTemplate IncomingDataTemplate { get; set; }
    public DataTemplate OutgoingDataTemplate { get; set; }

    public MyDataTemplateSelector()
    {
        this.incomingDataTemplate = new DataTemplate(typeof(IncomingViewCell));
        this.outgoingDataTemplate = new DataTemplate(typeof(OutgoingViewCell));  
    }

    protected override DataTemplate OnSelectTemplate(object item, BindableObject container)
    {
        var messageVm = item as Message;
        if (messageVm == null)
            return null;
        return messageVm.IsIncoming ? this.incomingDataTemplate : this.outgoingDataTemplate;
    }

    private readonly DataTemplate incomingDataTemplate;
    private readonly DataTemplate outgoingDataTemplate;
}

About

This sample shows how to use DataTemplateSelector in Xamarin Forms ListView

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 5

Languages