We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to enable compiled binding for Xamarin.Forms ListView (SfListView)

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: July 28, 2020 |
Last Revised Date: July 29, 2020

The Xamarin.Forms has introduced compiled bindings to reduce CPU cycles and boost performance when using data binding.

You can enable compiled binding by using the special x:DataType attribute on any VisualElement in Xamarin.Forms SfListView.

You can also refer to the following document regarding the compiled bindings in Xamarin.Forms,

https://devblogs.microsoft.com/xamarin/compiled-bindings-xamarin-forms/

XAML

Define the namespace of the ViewModel and set BindingContext for the ContentPage.

<ContentPage xmlns:viewModel="clr-namespace:ListViewXamarin.ViewModel"
             x:DataType="viewModel:ContactsViewModel">
    <ContentPage.BindingContext>
        <viewModel:ContactsViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}">
            </syncfusion:SfListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

When you are using template control like SfListView. you need to set up complied binding for the DataTemplate by using x:DataTye.

<ContentPage xmlns:model="clr-namespace:ListViewXamarin.Model">
    <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}">
        <syncfusion:SfListView.ItemTemplate>
            <DataTemplate x:DataType="model:Contacts">
                <Grid x:Name="grid">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="70" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                    <Image Source="{Binding ContactImage}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="50" WidthRequest="50"/>
                    <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center">
                        <Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/>
                        <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/>
                    </Grid>
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.ItemTemplate>
    </syncfusion:SfListView>
</ContentPage>

For SfListView.GroupHeaderTemplate, BindingContext of GroupHeaderTemplate will be the GroupResult that can be accessed from Syncfusion.DataSource.Extensions of Syncfusion.DataSource.

<ContentPage xmlns:result="clr-namespace:Syncfusion.DataSource.Extensions;assembly=Syncfusion.DataSource.Portable">
    <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}">
        <syncfusion:SfListView.GroupHeaderTemplate>
            <DataTemplate x:DataType="result:GroupResult">
                <Grid BackgroundColor="#E4E4E4">
                    <Label Text="{Binding Key}" FontSize="18" FontAttributes="Bold" VerticalOptions="Center" HorizontalOptions="Start" Margin="20,0,0,0" />
                </Grid>
            </DataTemplate>
        </syncfusion:SfListView.GroupHeaderTemplate>
    </syncfusion:SfListView>
</ContentPage>

Output

Demo image for using compiled binding for SfListView.itemTemplate and SfListView.GroupHeaderTemplate.

View sample in GitHub

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile